@@ -15,115 +15,89 @@ import (
15
15
)
16
16
17
17
func TestAccCachedImageDataSource (t * testing.T ) {
18
- t .Run ("Found" , func (t * testing.T ) {
19
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
20
- defer cancel ()
21
- files := map [string ]string {
22
- ".devcontainer/devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
23
- ".devcontainer/Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
18
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
19
+ defer cancel ()
20
+ files := map [string ]string {
21
+ ".devcontainer/devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
22
+ ".devcontainer/Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
24
23
RUN date > /date.txt` ,
25
- }
24
+ }
26
25
27
- deps := setup (ctx , t , files )
28
- seedCache (ctx , t , deps )
29
- deps .ExtraEnv ["FOO" ] = "bar"
30
- resource .Test (t , resource.TestCase {
31
- PreCheck : func () { testAccPreCheck (t ) },
32
- ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
33
- Steps : []resource.TestStep {
34
- // Plan create only
35
- {
36
- Config : deps .Config (t ),
37
- PlanOnly : true ,
38
- ExpectNonEmptyPlan : true , // TODO: check plan
39
- },
40
- // Create
41
- {
42
- Config : deps .Config (t ),
43
- Check : resource .ComposeAggregateTestCheckFunc (
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" ),
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
- // Computed
53
- resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "id" , func (value string ) error {
54
- // value is enclosed in quotes
55
- value = strings .Trim (value , `"` )
56
- if ! strings .HasPrefix (value , "sha256:" ) {
57
- return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
58
- }
59
- return nil
60
- }),
61
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "true" ),
62
- resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "image" ),
63
- resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "image" , func (value string ) error {
64
- // value is enclosed in quotes
65
- value = strings .Trim (value , `"` )
66
- if ! strings .HasPrefix (value , deps .CacheRepo ) {
67
- return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
68
- }
69
- return nil
70
- }),
71
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.0" , "FOO=\" bar\" " ),
72
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.1" , fmt .Sprintf ("ENVBUILDER_CACHE_REPO=%q" , deps .CacheRepo )),
73
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.2" , fmt .Sprintf ("ENVBUILDER_GIT_URL=%q" , deps .Repo .URL )),
74
- ),
75
- },
76
- // Should produce an empty plan after apply
77
- {
78
- Config : deps .Config (t ),
79
- PlanOnly : true ,
80
- },
26
+ deps := setup (ctx , t , files )
27
+ deps .ExtraEnv ["FOO" ] = "bar"
28
+ resource .Test (t , resource.TestCase {
29
+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
30
+ Steps : []resource.TestStep {
31
+ // Initial state: cache has not been seeded.
32
+ {
33
+ Config : deps .Config (t ),
34
+ PlanOnly : true ,
35
+ ExpectNonEmptyPlan : true ,
81
36
},
82
- })
83
- })
84
-
85
- t .Run ("NotFound" , func (t * testing.T ) {
86
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
87
- defer cancel ()
88
- files := map [string ]string {
89
- ".devcontainer/devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
90
- ".devcontainer/Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
91
- RUN date > /date.txt` ,
92
- }
93
- deps := setup (ctx , t , files )
94
- deps .ExtraEnv ["FOO" ] = "bar"
95
- // We do not seed the cache.
96
- resource .Test (t , resource.TestCase {
97
- PreCheck : func () { testAccPreCheck (t ) },
98
- ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
99
- Steps : []resource.TestStep {
100
- // Should produce a non-empty plan.
101
- {
102
- Config : deps .Config (t ),
103
- PlanOnly : true ,
104
- ExpectNonEmptyPlan : true ,
105
- },
106
- // Should detect that no cached image is present.
107
- {
108
- Config : deps .Config (t ),
109
- Check : resource .ComposeAggregateTestCheckFunc (
110
- // Computed values MUST be present.
111
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
112
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
113
- resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
114
- // Cached image should be set to the builder image.
115
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
116
- // Inputs should still be present.
117
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
118
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
119
- resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
120
- // Should be empty
121
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
122
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
123
- resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
124
- ),
37
+ // Should detect that no cached image is present.
38
+ {
39
+ Config : deps .Config (t ),
40
+ Check : resource .ComposeAggregateTestCheckFunc (
41
+ // Computed values MUST be present.
42
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "id" , uuid .Nil .String ()),
43
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "false" ),
44
+ resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "env.0" ),
45
+ // Cached image should be set to the builder image.
46
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "image" , deps .BuilderImage ),
47
+ // Inputs should still be present.
48
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
49
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
50
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
51
+ // Should be empty
52
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
53
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
54
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
55
+ ),
56
+ },
57
+ // Now, seed the cache. We should detect the cached image resource.
58
+ {
59
+ PreConfig : func () {
60
+ seedCache (ctx , t , deps )
125
61
},
62
+ Config : deps .Config (t ),
63
+ Check : resource .ComposeAggregateTestCheckFunc (
64
+ // Inputs should still be present.
65
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
66
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
67
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "git_url" , deps .Repo .URL ),
68
+ // Should be empty
69
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_username" ),
70
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "git_password" ),
71
+ resource .TestCheckNoResourceAttr ("envbuilder_cached_image.test" , "cache_ttl_days" ),
72
+ // Computed
73
+ resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "id" , func (value string ) error {
74
+ // value is enclosed in quotes
75
+ value = strings .Trim (value , `"` )
76
+ if ! strings .HasPrefix (value , "sha256:" ) {
77
+ return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
78
+ }
79
+ return nil
80
+ }),
81
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "exists" , "true" ),
82
+ resource .TestCheckResourceAttrSet ("envbuilder_cached_image.test" , "image" ),
83
+ resource .TestCheckResourceAttrWith ("envbuilder_cached_image.test" , "image" , func (value string ) error {
84
+ // value is enclosed in quotes
85
+ value = strings .Trim (value , `"` )
86
+ if ! strings .HasPrefix (value , deps .CacheRepo ) {
87
+ return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
88
+ }
89
+ return nil
90
+ }),
91
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.0" , "FOO=\" bar\" " ),
92
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.1" , fmt .Sprintf ("ENVBUILDER_CACHE_REPO=%q" , deps .CacheRepo )),
93
+ resource .TestCheckResourceAttr ("envbuilder_cached_image.test" , "env.2" , fmt .Sprintf ("ENVBUILDER_GIT_URL=%q" , deps .Repo .URL )),
94
+ ),
95
+ },
96
+ // Should produce an empty plan after apply
97
+ {
98
+ Config : deps .Config (t ),
99
+ PlanOnly : true ,
126
100
},
127
- })
101
+ },
128
102
})
129
103
}
0 commit comments