@@ -10,106 +10,129 @@ import (
10
10
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11
11
)
12
12
13
+ // testEnvValue is a multi-line environment variable value that we use in
14
+ // tests to ensure that we can handle multi-line values correctly.
15
+ var testEnvValue = `bar
16
+ baz`
17
+
13
18
func TestAccCachedImageResource (t * testing.T ) {
14
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
19
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Minute )
15
20
defer cancel ()
16
- files := map [string ]string {
17
- ".devcontainer/devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
18
- ".devcontainer/Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
19
- RUN date > /date.txt` ,
20
- }
21
21
22
- deps := setup (ctx , t , files )
23
- deps .ExtraEnv ["FOO" ] = `bar
24
- baz` // THIS IS A LOAD-BEARING NEWLINE. DO NOT REMOVE.
25
- resource .Test (t , resource.TestCase {
26
- ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
27
- Steps : []resource.TestStep {
28
- // Initial state: cache has not been seeded.
29
- {
30
- Config : deps .Config (t ),
31
- PlanOnly : true ,
32
- ExpectNonEmptyPlan : true ,
33
- },
34
- // Should detect that no cached image is present and plan to create the resource.
35
- {
36
- Config : deps .Config (t ),
37
- Check : resource .ComposeAggregateTestCheckFunc (
38
- // Computed values MUST be present.
39
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
40
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
41
- resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
42
- // Cached image should be set to the builder image.
43
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
44
- // Inputs should still be present.
45
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
46
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
47
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
48
- // Should be empty
49
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
50
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
51
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
52
- ),
53
- ExpectNonEmptyPlan : true , // TODO: check the plan.
54
- },
55
- // Re-running plan should have the same effect.
56
- {
57
- Config : deps .Config (t ),
58
- Check : resource .ComposeAggregateTestCheckFunc (
59
- // Computed values MUST be present.
60
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
61
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
62
- resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
63
- // Cached image should be set to the builder image.
64
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
65
- // Inputs should still be present.
66
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
67
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
68
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
69
- // Should be empty
70
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
71
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
72
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
73
- ),
74
- ExpectNonEmptyPlan : true , // TODO: check the plan.
75
- },
76
- // Now, seed the cache and re-run. We should now successfully create the cached image resource.
77
- {
78
- PreConfig : func () {
79
- seedCache (ctx , t , deps )
80
- },
81
- Config : deps .Config (t ),
82
- Check : resource .ComposeAggregateTestCheckFunc (
83
- // Inputs should still be present.
84
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
85
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
86
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
87
- // Should be empty
88
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
89
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
90
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
91
- // Computed
92
- resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "id" , quotedPrefix ("sha256:" )),
93
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "true" ),
94
- resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "image" ),
95
- resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "image" , quotedPrefix (deps .CacheRepo )),
96
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.0" , "FOO=bar\n baz" ),
97
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.1" , fmt .Sprintf ("ENVBUILDER_CACHE_REPO=%s" , deps .CacheRepo )),
98
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.2" , fmt .Sprintf ("ENVBUILDER_GIT_URL=%s" , deps .Repo .URL )),
99
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.3" , "ENVBUILDER_REMOTE_REPO_BUILD_MODE=true" ),
100
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "env.4" ),
101
- ),
22
+ for _ , tc := range []struct {
23
+ name string
24
+ files map [string ]string
25
+ }{
26
+ {
27
+ name : "devcontainer only" ,
28
+ files : map [string ]string {
29
+ ".devcontainer/devcontainer.json" : `{"image": "localhost:5000/test-ubuntu:latest"}` ,
102
30
},
103
- // Should produce an empty plan after apply
104
- {
105
- Config : deps .Config (t ),
106
- PlanOnly : true ,
107
- },
108
- // Ensure idempotence in this state!
109
- {
110
- Config : deps .Config (t ),
111
- PlanOnly : true ,
31
+ },
32
+ {
33
+ name : "devcontainer and Dockerfile" ,
34
+ files : map [string ]string {
35
+ ".devcontainer/devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
36
+ ".devcontainer/Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
37
+ RUN date > /date.txt` ,
112
38
},
113
39
},
114
- })
40
+ } {
41
+ t .Run (tc .name , func (t * testing.T ) {
42
+ //nolint: paralleltest
43
+ deps := setup (ctx , t , tc .files )
44
+ deps .ExtraEnv ["FOO" ] = testEnvValue
45
+
46
+ resource .Test (t , resource.TestCase {
47
+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
48
+ Steps : []resource.TestStep {
49
+ // Initial state: cache has not been seeded.
50
+ {
51
+ Config : deps .Config (t ),
52
+ PlanOnly : true ,
53
+ ExpectNonEmptyPlan : true ,
54
+ },
55
+ // Should detect that no cached image is present and plan to create the resource.
56
+ {
57
+ Config : deps .Config (t ),
58
+ Check : resource .ComposeAggregateTestCheckFunc (
59
+ // Computed values MUST be present.
60
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
61
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
62
+ resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
63
+ // Cached image should be set to the builder image.
64
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
65
+ // Inputs should still be present.
66
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
67
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
68
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
69
+ // Should be empty
70
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
71
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
72
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
73
+ ),
74
+ ExpectNonEmptyPlan : true , // TODO: check the plan.
75
+ },
76
+ // Re-running plan should have the same effect.
77
+ {
78
+ Config : deps .Config (t ),
79
+ Check : resource .ComposeAggregateTestCheckFunc (
80
+ // Computed values MUST be present.
81
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
82
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
83
+ resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
84
+ // Cached image should be set to the builder image.
85
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
86
+ // Inputs should still be present.
87
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
88
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
89
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
90
+ // Should be empty
91
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
92
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
93
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
94
+ ),
95
+ ExpectNonEmptyPlan : true , // TODO: check the plan.
96
+ },
97
+ // Now, seed the cache and re-run. We should now successfully create the cached image resource.
98
+ {
99
+ PreConfig : func () {
100
+ seedCache (ctx , t , deps )
101
+ },
102
+ Config : deps .Config (t ),
103
+ Check : resource .ComposeAggregateTestCheckFunc (
104
+ // Inputs should still be present.
105
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
106
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar\n baz" ),
107
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
108
+ // Should be empty
109
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
110
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
111
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
112
+ // Computed
113
+ resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "id" , quotedPrefix ("sha256:" )),
114
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "true" ),
115
+ resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "image" ),
116
+ resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "image" , quotedPrefix (deps .CacheRepo )),
117
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.0" , "FOO=bar\n baz" ),
118
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.1" , fmt .Sprintf ("ENVBUILDER_CACHE_REPO=%s" , deps .CacheRepo )),
119
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.2" , fmt .Sprintf ("ENVBUILDER_GIT_URL=%s" , deps .Repo .URL )),
120
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.3" , "ENVBUILDER_REMOTE_REPO_BUILD_MODE=true" ),
121
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "env.4" ),
122
+ ),
123
+ },
124
+ // Should produce an empty plan after apply
125
+ {
126
+ Config : deps .Config (t ),
127
+ PlanOnly : true ,
128
+ },
129
+ // Ensure idempotence in this state!
130
+ {
131
+ Config : deps .Config (t ),
132
+ PlanOnly : true ,
133
+ },
134
+ },
135
+ })
136
+ })
137
+ }
115
138
}
0 commit comments