1// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package remoteexec
16
17import (
18	"fmt"
19	"testing"
20)
21
22func TestTemplate(t *testing.T) {
23	tests := []struct {
24		name   string
25		params *REParams
26		want   string
27	}{
28		{
29			name: "basic",
30			params: &REParams{
31				Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
32				Inputs:      []string{"$in"},
33				OutputFiles: []string{"$out"},
34				Platform: map[string]string{
35					ContainerImageKey: DefaultImage,
36					PoolKey:           "default",
37				},
38			},
39			want: fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out --env_var_allowlist=LANG,LC_MESSAGES,PYTHONDONTWRITEBYTECODE -- ", DefaultImage),
40		},
41		{
42			name: "all params",
43			params: &REParams{
44				Labels:          map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
45				Inputs:          []string{"$in"},
46				OutputFiles:     []string{"$out"},
47				ExecStrategy:    "remote",
48				RSPFiles:        []string{"$out.rsp", "out2.rsp"},
49				ToolchainInputs: []string{"clang++"},
50				Platform: map[string]string{
51					ContainerImageKey: DefaultImage,
52					PoolKey:           "default",
53				},
54			},
55			want: fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=remote --inputs=$in --input_list_paths=$out.rsp,out2.rsp --output_files=$out --toolchain_inputs=clang++ --env_var_allowlist=LANG,LC_MESSAGES,PYTHONDONTWRITEBYTECODE -- ", DefaultImage),
56		},
57	}
58	for _, test := range tests {
59		t.Run(test.name, func(t *testing.T) {
60			if got := test.params.Template(); got != test.want {
61				t.Errorf("Template() returned\n%s\nwant\n%s", got, test.want)
62			}
63		})
64	}
65}
66
67func TestNoVarTemplate(t *testing.T) {
68	params := &REParams{
69		Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
70		Inputs:      []string{"$in"},
71		OutputFiles: []string{"$out"},
72		Platform: map[string]string{
73			ContainerImageKey: DefaultImage,
74			PoolKey:           "default",
75		},
76	}
77	want := fmt.Sprintf("prebuilts/remoteexecution-client/live/rewrapper --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out --env_var_allowlist=LANG,LC_MESSAGES,PYTHONDONTWRITEBYTECODE -- ", DefaultImage)
78	if got := params.NoVarTemplate(DefaultWrapperPath); got != want {
79		t.Errorf("NoVarTemplate() returned\n%s\nwant\n%s", got, want)
80	}
81}
82
83func TestTemplateDeterminism(t *testing.T) {
84	r := &REParams{
85		Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
86		Inputs:      []string{"$in"},
87		OutputFiles: []string{"$out"},
88		Platform: map[string]string{
89			ContainerImageKey: DefaultImage,
90			PoolKey:           "default",
91		},
92	}
93	want := fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out --env_var_allowlist=LANG,LC_MESSAGES,PYTHONDONTWRITEBYTECODE -- ", DefaultImage)
94	for i := 0; i < 1000; i++ {
95		if got := r.Template(); got != want {
96			t.Fatalf("Template() returned\n%s\nwant\n%s", got, want)
97		}
98	}
99}
100