File: post-processor_test.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (183 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download | duplicates (2)
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
package yandexexport

import (
	"testing"

	"github.com/hashicorp/packer/builder/yandex"
	"github.com/stretchr/testify/require"

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
)

func TestPostProcessor_Configure(t *testing.T) {
	type fields struct {
		config Config
		runner multistep.Runner
	}
	type args struct {
		raws []interface{}
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		wantErr bool
	}{
		{
			name: "no one creds",
			fields: fields{
				config: Config{
					AccessConfig: yandex.AccessConfig{
						Token:                 "",
						ServiceAccountKeyFile: "",
					},
					ExchangeConfig: ExchangeConfig{
						ServiceAccountID: "some-srv-acc-id",
					},
					CommonConfig: yandex.CommonConfig{
						CloudConfig: yandex.CloudConfig{
							FolderID: "some-folder-id",
						},
					},
				},
			},
			wantErr: false,
		},
		{
			name: "both token and sa key file",
			fields: fields{
				config: Config{
					AccessConfig: yandex.AccessConfig{
						Token:                 "some-value",
						ServiceAccountKeyFile: "path/not-exist.file",
					},
					ExchangeConfig: ExchangeConfig{
						ServiceAccountID: "some-srv-acc-id",
					},
					CommonConfig: yandex.CommonConfig{
						CloudConfig: yandex.CloudConfig{
							FolderID: "some-folder-id",
						},
					},
				},
			},
			wantErr: true,
		},
		{
			name: "use sa key file",
			fields: fields{
				config: Config{
					AccessConfig: yandex.AccessConfig{
						Token:                 "",
						ServiceAccountKeyFile: "testdata/fake-sa-key.json",
					},
					ExchangeConfig: ExchangeConfig{
						ServiceAccountID: "some-srv-acc-id",
					},
					CommonConfig: yandex.CommonConfig{
						CloudConfig: yandex.CloudConfig{
							FolderID: "some-folder-id",
						},
					},
				},
			},
			wantErr: false,
		},
		{
			name: "service_account_id required",
			fields: fields{
				config: Config{
					AccessConfig: yandex.AccessConfig{
						Token: "some token",
					},
					ExchangeConfig: ExchangeConfig{
						ServiceAccountID: "",
					},
					CommonConfig: yandex.CommonConfig{
						CloudConfig: yandex.CloudConfig{
							FolderID: "some-folder-id",
						},
					},
				},
			},
			wantErr: true,
		},
		{
			name: "folderID required",
			fields: fields{
				config: Config{
					AccessConfig: yandex.AccessConfig{
						Token: "some token",
					},
					ExchangeConfig: ExchangeConfig{
						ServiceAccountID: "some-srv-acc-id",
					},
					CommonConfig: yandex.CommonConfig{
						CloudConfig: yandex.CloudConfig{
							FolderID: "",
						},
					},
				},
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.fields.config.Paths = []string{"some-path"} // make Paths not empty
			p := &PostProcessor{
				config: tt.fields.config,
				runner: tt.fields.runner,
			}
			if err := p.Configure(tt.args.raws...); (err != nil) != tt.wantErr {
				t.Errorf("Configure() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func Test_formUrls(t *testing.T) {
	type args struct {
		paths []string
	}
	tests := []struct {
		name       string
		args       args
		wantResult []string
	}{
		{
			name: "empty list",
			args: args{
				paths: []string{},
			},
			wantResult: []string{},
		},
		{
			name: "one element",
			args: args{
				paths: []string{"s3://bucket1/object1"},
			},
			wantResult: []string{"https://" + defaultStorageEndpoint + "/bucket1/object1"},
		},
		{
			name: "several elements",
			args: args{
				paths: []string{
					"s3://bucket1/object1",
					"s3://bucket-name/object-with/prefix/filename.blob",
					"s3://bucket-too/foo/bar.test",
				},
			},
			wantResult: []string{
				"https://" + defaultStorageEndpoint + "/bucket1/object1",
				"https://" + defaultStorageEndpoint + "/bucket-name/object-with/prefix/filename.blob",
				"https://" + defaultStorageEndpoint + "/bucket-too/foo/bar.test",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			require.Equal(t, tt.wantResult, formUrls(tt.args.paths))
		})
	}
}