-
Notifications
You must be signed in to change notification settings - Fork 167
/
bicep_test.go
213 lines (174 loc) · 5.39 KB
/
bicep_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package bicep
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockinput"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewBicepCli(t *testing.T) {
configRoot := t.TempDir()
t.Setenv("AZD_CONFIG_DIR", configRoot)
mockContext := mocks.NewMockContext(context.Background())
mockContext.HttpClient.When(func(request *http.Request) bool {
return request.Method == http.MethodGet && request.URL.Host == "downloads.bicep.azure.com"
}).Respond(&http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("this is bicep")),
})
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
return strings.Contains(args.Cmd, "bicep") && len(args.Args) == 1 && args.Args[0] == "--version"
}).Respond(exec.NewRunResult(
0,
fmt.Sprintf("Bicep CLI version %s (abcdef0123)", BicepVersion.String()),
"",
))
cli, err := newBicepCliWithTransporter(
*mockContext.Context, mockContext.Console, mockContext.CommandRunner, mockContext.HttpClient,
)
require.NoError(t, err)
require.NotNil(t, cli)
require.Equal(t, 2, len(mockContext.Console.SpinnerOps()))
require.Equal(t, mockinput.SpinnerOp{
Op: mockinput.SpinnerOpShow,
Message: "Downloading Bicep",
Format: input.Step,
}, mockContext.Console.SpinnerOps()[0])
require.Equal(t, mockinput.SpinnerOp{
Op: mockinput.SpinnerOpStop,
Message: "Downloading Bicep",
Format: input.StepDone,
}, mockContext.Console.SpinnerOps()[1])
bicepPath, err := azdBicepPath()
require.NoError(t, err)
contents, err := os.ReadFile(bicepPath)
require.NoError(t, err)
require.Equal(t, []byte("this is bicep"), contents)
}
func TestNewBicepCliWillUpgrade(t *testing.T) {
const OLD_FILE_CONTENTS = "this is OLD bicep"
const NEW_FILE_CONTENTS = "this is NEW bicep"
configRoot := t.TempDir()
t.Setenv("AZD_CONFIG_DIR", configRoot)
bicepPath, err := azdBicepPath()
require.NoError(t, err)
err = os.MkdirAll(filepath.Dir(bicepPath), osutil.PermissionDirectory)
require.NoError(t, err)
err = os.WriteFile(bicepPath, []byte(OLD_FILE_CONTENTS), osutil.PermissionExecutableFile)
require.NoError(t, err)
mockContext := mocks.NewMockContext(context.Background())
mockContext.HttpClient.When(func(request *http.Request) bool {
return request.Method == http.MethodGet && request.URL.Host == "downloads.bicep.azure.com"
}).Respond(&http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(NEW_FILE_CONTENTS)),
})
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
return strings.Contains(args.Cmd, "bicep") && len(args.Args) == 1 && args.Args[0] == "--version"
}).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) {
contents, err := os.ReadFile(bicepPath)
if err != nil {
return exec.NewRunResult(-1, "", "couldn't read bicep file"), err
}
switch string(contents) {
case OLD_FILE_CONTENTS:
return exec.NewRunResult(0, "Bicep CLI version 0.0.1 (badbadbad1)", ""), nil
case NEW_FILE_CONTENTS:
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", BicepVersion.String()), ""), nil
}
return exec.NewRunResult(-1, "", "unexpected bicep file contents"), err
})
cli, err := newBicepCliWithTransporter(
*mockContext.Context, mockContext.Console, mockContext.CommandRunner, mockContext.HttpClient,
)
require.NoError(t, err)
require.NotNil(t, cli)
require.Equal(t, 2, len(mockContext.Console.SpinnerOps()))
require.Equal(t, mockinput.SpinnerOp{
Op: mockinput.SpinnerOpShow,
Message: "Upgrading Bicep",
Format: input.Step,
}, mockContext.Console.SpinnerOps()[0])
require.Equal(t, mockinput.SpinnerOp{
Op: mockinput.SpinnerOpStop,
Message: "Upgrading Bicep",
Format: input.StepDone,
}, mockContext.Console.SpinnerOps()[1])
contents, err := os.ReadFile(bicepPath)
require.NoError(t, err)
require.Equal(t, []byte(NEW_FILE_CONTENTS), contents)
}
func Test_preferMuslBicep(t *testing.T) {
tests := []struct {
name string
hasMusl bool
hasGlibc bool
want bool
}{
{
name: "musl preferred",
hasMusl: true,
hasGlibc: false,
want: true,
},
{
name: "glibc preferred",
hasMusl: false,
hasGlibc: true,
want: false,
},
{
name: "both available",
hasMusl: true,
hasGlibc: true,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockStat := func(name string) (os.FileInfo, error) {
if tt.hasMusl && name == "/lib/ld-musl-x86_64.so.1" {
return &fakeFileInfo{}, nil
}
if tt.hasGlibc && name == "/lib/x86_64-linux-gnu/libc.so.6" {
return &fakeFileInfo{}, nil
}
return nil, os.ErrNotExist
}
got := preferMuslBicep(mockStat)
assert.Equal(t, tt.want, got)
})
}
}
type fakeFileInfo struct {
}
func (f *fakeFileInfo) Name() string {
return ""
}
func (f *fakeFileInfo) Size() int64 {
return 0
}
func (f *fakeFileInfo) Mode() os.FileMode {
return 0
}
func (f *fakeFileInfo) ModTime() time.Time {
return time.Time{}
}
func (f *fakeFileInfo) IsDir() bool {
return false
}
func (f *fakeFileInfo) Sys() interface{} {
return nil
}