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	"android/soong/android"
22)
23
24func TestTemplate(t *testing.T) {
25	tests := []struct {
26		name   string
27		params *REParams
28		want   string
29	}{
30		{
31			name: "basic",
32			params: &REParams{
33				Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
34				Inputs:      []string{"$in"},
35				OutputFiles: []string{"$out"},
36				Platform: map[string]string{
37					ContainerImageKey: DefaultImage,
38					PoolKey:           "default",
39				},
40			},
41			want: fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage),
42		},
43		{
44			name: "all params",
45			params: &REParams{
46				Labels:          map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
47				Inputs:          []string{"$in"},
48				OutputFiles:     []string{"$out"},
49				ExecStrategy:    "remote",
50				RSPFile:         "$out.rsp",
51				ToolchainInputs: []string{"clang++"},
52				Platform: map[string]string{
53					ContainerImageKey: DefaultImage,
54					PoolKey:           "default",
55				},
56			},
57			want: fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=remote --inputs=$in --input_list_paths=$out.rsp --output_files=$out --toolchain_inputs=clang++ -- ", DefaultImage),
58		},
59	}
60	for _, test := range tests {
61		t.Run(test.name, func(t *testing.T) {
62			if got := test.params.Template(); got != test.want {
63				t.Errorf("Template() returned\n%s\nwant\n%s", got, test.want)
64			}
65		})
66	}
67}
68
69func TestNoVarTemplate(t *testing.T) {
70	params := &REParams{
71		Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
72		Inputs:      []string{"$in"},
73		OutputFiles: []string{"$out"},
74		Platform: map[string]string{
75			ContainerImageKey: DefaultImage,
76			PoolKey:           "default",
77		},
78	}
79	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 -- ", DefaultImage)
80	if got := params.NoVarTemplate(android.NullConfig("")); got != want {
81		t.Errorf("NoVarTemplate() returned\n%s\nwant\n%s", got, want)
82	}
83}
84
85func TestTemplateDeterminism(t *testing.T) {
86	r := &REParams{
87		Labels:      map[string]string{"type": "compile", "lang": "cpp", "compiler": "clang"},
88		Inputs:      []string{"$in"},
89		OutputFiles: []string{"$out"},
90		Platform: map[string]string{
91			ContainerImageKey: DefaultImage,
92			PoolKey:           "default",
93		},
94	}
95	want := fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage)
96	for i := 0; i < 1000; i++ {
97		if got := r.Template(); got != want {
98			t.Fatalf("Template() returned\n%s\nwant\n%s", got, want)
99		}
100	}
101}
102