@@ -16,15 +16,75 @@ import (
16
16
// TODO: change this to only test for a non-existent image.
17
17
// Move the heavy lifting to integration.
18
18
func TestAccCachedImageDataSource (t * testing.T ) {
19
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
20
- t .Cleanup (cancel )
21
- files := map [string ]string {
22
- "devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
23
- "Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
19
+ t .Run ("Found" , func (t * testing.T ) {
20
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
21
+ t .Cleanup (cancel )
22
+ files := map [string ]string {
23
+ "devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
24
+ "Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
24
25
RUN apt-get update && apt-get install -y cowsay` ,
26
+ }
27
+ deps := setup (t , files )
28
+ seedCache (ctx , t , deps )
29
+ tfCfg := fmt .Sprintf (`data "envbuilder_cached_image" "test" {
30
+ builder_image = %q
31
+ devcontainer_dir = %q
32
+ git_url = %q
33
+ extra_env = {
34
+ "FOO" : "bar"
25
35
}
26
- deps := setup (ctx , t , files )
27
- tfCfg := fmt .Sprintf (`data "envbuilder_cached_image" "test" {
36
+ cache_repo = %q
37
+ }` , deps .BuilderImage , deps .RepoDir , deps .RepoDir , deps .CacheRepo )
38
+ resource .Test (t , resource.TestCase {
39
+ PreCheck : func () { testAccPreCheck (t ) },
40
+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
41
+ Steps : []resource.TestStep {
42
+ {
43
+ Config : tfCfg ,
44
+ Check : resource .ComposeAggregateTestCheckFunc (
45
+ // Inputs should still be present.
46
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
47
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
48
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "git_url" , deps .RepoDir ),
49
+ // Should be empty
50
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_username" ),
51
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_password" ),
52
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "cache_ttl_days" ),
53
+ // Computed
54
+ resource .TestCheckResourceAttrWith ("data.envbuilder_cached_image.test" , "id" , func (value string ) error {
55
+ // value is enclosed in quotes
56
+ value = strings .Trim (value , `"` )
57
+ if ! strings .HasPrefix (value , "sha256:" ) {
58
+ return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
59
+ }
60
+ return nil
61
+ }),
62
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "exists" , "true" ),
63
+ resource .TestCheckResourceAttrSet ("data.envbuilder_cached_image.test" , "image" ),
64
+ resource .TestCheckResourceAttrWith ("data.envbuilder_cached_image.test" , "image" , func (value string ) error {
65
+ // value is enclosed in quotes
66
+ value = strings .Trim (value , `"` )
67
+ if ! strings .HasPrefix (value , deps .CacheRepo ) {
68
+ return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
69
+ }
70
+ return nil
71
+ }),
72
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "env.0" , "FOO=\" bar\" " ),
73
+ ),
74
+ },
75
+ },
76
+ })
77
+ })
78
+
79
+ t .Run ("NotFound" , func (t * testing.T ) {
80
+ files := map [string ]string {
81
+ "devcontainer.json" : `{"build": { "dockerfile": "Dockerfile" }}` ,
82
+ "Dockerfile" : `FROM localhost:5000/test-ubuntu:latest
83
+ RUN apt-get update && apt-get install -y cowsay` ,
84
+ }
85
+ deps := setup (t , files )
86
+ // We do not seed the cache.
87
+ tfCfg := fmt .Sprintf (`data "envbuilder_cached_image" "test" {
28
88
builder_image = %q
29
89
devcontainer_dir = %q
30
90
git_url = %q
@@ -33,37 +93,29 @@ func TestAccCachedImageDataSource(t *testing.T) {
33
93
}
34
94
cache_repo = %q
35
95
}` , deps .BuilderImage , deps .RepoDir , deps .RepoDir , deps .CacheRepo )
36
- resource .Test (t , resource.TestCase {
37
- PreCheck : func () { testAccPreCheck (t ) },
38
- ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
39
- Steps : []resource.TestStep {
40
- // Read testing
41
- {
42
- Config : tfCfg ,
43
- Check : resource .ComposeAggregateTestCheckFunc (
44
- // Input
45
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
46
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
47
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "git_url" , deps .RepoDir ),
48
- // Should be empty
49
- resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_username" ),
50
- resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_password" ),
51
- resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "cache_ttl_days" ),
52
- // Computed
53
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "id" , "cached-image-id" ),
54
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "exists" , "true" ),
55
- resource .TestCheckResourceAttrSet ("data.envbuilder_cached_image.test" , "image" ),
56
- resource .TestCheckResourceAttrWith ("data.envbuilder_cached_image.test" , "image" , func (value string ) error {
57
- // value is enclosed in quotes
58
- value = strings .Trim (value , `"` )
59
- if ! strings .HasPrefix (value , deps .CacheRepo ) {
60
- return fmt .Errorf ("expected image %q to have prefix %q" , value , deps .CacheRepo )
61
- }
62
- return nil
63
- }),
64
- resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "env.0" , "FOO=\" bar\" " ),
65
- ),
96
+ resource .Test (t , resource.TestCase {
97
+ PreCheck : func () { testAccPreCheck (t ) },
98
+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
99
+ Steps : []resource.TestStep {
100
+ {
101
+ Config : tfCfg ,
102
+ Check : resource .ComposeAggregateTestCheckFunc (
103
+ // Inputs should still be present.
104
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "cache_repo" , deps .CacheRepo ),
105
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "extra_env.FOO" , "bar" ),
106
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "git_url" , deps .RepoDir ),
107
+ // Should be empty
108
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_username" ),
109
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "git_password" ),
110
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "cache_ttl_days" ),
111
+ // Computed values should be empty.
112
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "id" ),
113
+ resource .TestCheckResourceAttr ("data.envbuilder_cached_image.test" , "exists" , "false" ),
114
+ resource .TestCheckNoResourceAttr ("data.envbuilder_cached_image.test" , "image" ),
115
+ resource .TestCheckResourceAttrSet ("data.envbuilder_cached_image.test" , "env.0" ),
116
+ ),
117
+ },
66
118
},
67
- },
119
+ })
68
120
})
69
121
}
0 commit comments