Skip to content

Commit 723553f

Browse files
committed
step 2: make the tests pass
1 parent b3f9e5d commit 723553f

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

internal/provider/cached_image_resource.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ func (r *CachedImageResource) Read(ctx context.Context, req resource.ReadRequest
246246
}
247247

248248
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
249-
return
250249
}
251250

252251
func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
@@ -259,10 +258,9 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
259258
return
260259
}
261260

262-
// Create a random UUID for the resource type.
263-
data.ID = types.StringValue(uuid.NewString())
264-
265261
cachedImg, err := r.runCacheProbe(ctx, data)
262+
data.ID = types.StringValue(uuid.Nil.String())
263+
data.Exists = types.BoolValue(err == nil)
266264
if err != nil {
267265
// FIXME: there are legit errors that can crop up here.
268266
// We should add a sentinel error in Kaniko for uncached layers, and check
@@ -276,7 +274,7 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
276274
} else {
277275
tflog.Info(ctx, fmt.Sprintf("found image: %s@%s", data.CacheRepo.ValueString(), digest))
278276
data.Image = types.StringValue(fmt.Sprintf("%s@%s", data.CacheRepo.ValueString(), digest))
279-
data.Exists = types.BoolValue(err == nil)
277+
data.ID = types.StringValue(digest.String())
280278
}
281279
// Compute the env attribute from the config map.
282280
// TODO(mafredri): Convert any other relevant attributes given via schema.
@@ -295,11 +293,9 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
295293
}
296294

297295
func (r *CachedImageResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
298-
299296
}
300297

301298
func (r *CachedImageResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
302-
303299
}
304300

305301
// runCacheProbe performs a 'fake build' of the requested image and ensures that
@@ -333,7 +329,7 @@ func (r *CachedImageResource) runCacheProbe(ctx context.Context, data CachedImag
333329
// In order to correctly reproduce the final layer of the cached image, we
334330
// need the envbuilder binary used to originally build the image!
335331
envbuilderPath := filepath.Join(tmpDir, "envbuilder")
336-
if err := r.extractEnvbuilderFromImage(ctx, data.BuilderImage.ValueString(), envbuilderPath); err != nil {
332+
if err := extractEnvbuilderFromImage(ctx, data.BuilderImage.ValueString(), envbuilderPath); err != nil {
337333
tflog.Error(ctx, "failed to fetch envbuilder binary from builder image", map[string]any{"err": err})
338334
return nil, fmt.Errorf("Failed to fetch the envbuilder binary from the builder image: %s", err.Error())
339335
}
@@ -401,14 +397,14 @@ func (r *CachedImageResource) runCacheProbe(ctx context.Context, data CachedImag
401397

402398
// extractEnvbuilderFromImage reads the image located at imgRef and extracts
403399
// MagicBinaryLocation to destPath.
404-
func (r *CachedImageResource) extractEnvbuilderFromImage(ctx context.Context, imgRef, destPath string) error {
400+
func extractEnvbuilderFromImage(ctx context.Context, imgRef, destPath string) error {
405401
needle := filepath.Clean(constants.MagicBinaryLocation)[1:] // skip leading '/'
406402
ref, err := name.ParseReference(imgRef)
407403
if err != nil {
408404
return fmt.Errorf("parse reference: %w", err)
409405
}
410406

411-
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithTransport(r.client.Transport))
407+
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
412408
if err != nil {
413409
return fmt.Errorf("check remote image: %w", err)
414410
}

internal/provider/cached_image_resource_test.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/google/uuid"
1314
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1415
)
1516

@@ -22,7 +23,7 @@ func TestAccCachedImageDataSource(t *testing.T) {
2223
files := map[string]string{
2324
".devcontainer/devcontainer.json": `{"build": { "dockerfile": "Dockerfile" }}`,
2425
".devcontainer/Dockerfile": `FROM localhost:5000/test-ubuntu:latest
25-
RUN apt-get update && apt-get install -y cowsay`,
26+
RUN date > /date.txt`,
2627
}
2728

2829
deps := setup(ctx, t, files)
@@ -37,7 +38,7 @@ func TestAccCachedImageDataSource(t *testing.T) {
3738
}
3839
cache_repo = %q
3940
verbose = true
40-
}`, deps.BuilderImage, "/workspace", deps.Repo.URL, deps.Repo.Key, deps.CacheRepo)
41+
}`, deps.BuilderImage, deps.Repo.Dir, deps.Repo.URL, deps.Repo.Key, deps.CacheRepo)
4142
resource.Test(t, resource.TestCase{
4243
PreCheck: func() { testAccPreCheck(t) },
4344
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
@@ -85,7 +86,7 @@ func TestAccCachedImageDataSource(t *testing.T) {
8586
files := map[string]string{
8687
".devcontainer/devcontainer.json": `{"build": { "dockerfile": "Dockerfile" }}`,
8788
".devcontainer/Dockerfile": `FROM localhost:5000/test-ubuntu:latest
88-
RUN apt-get update && apt-get install -y cowsay`,
89+
RUN date > /date.txt`,
8990
}
9091
deps := setup(ctx, t, files)
9192
// We do not seed the cache.
@@ -99,27 +100,28 @@ func TestAccCachedImageDataSource(t *testing.T) {
99100
}
100101
cache_repo = %q
101102
verbose = true
102-
}`, deps.BuilderImage, "/workspace", deps.Repo.URL, deps.Repo.Key, deps.CacheRepo)
103+
}`, deps.BuilderImage, deps.Repo.Dir, deps.Repo.URL, deps.Repo.Key, deps.CacheRepo)
103104
resource.Test(t, resource.TestCase{
104105
PreCheck: func() { testAccPreCheck(t) },
105106
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
106107
Steps: []resource.TestStep{
107108
{
108109
Config: tfCfg,
109110
Check: resource.ComposeAggregateTestCheckFunc(
111+
// Computed values MUST be present.
112+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "id", uuid.Nil.String()),
113+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "exists", "false"),
114+
resource.TestCheckResourceAttrSet("envbuilder_cached_image.test", "env.0"),
115+
// Cached image should be set to the builder image.
116+
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "image", deps.BuilderImage),
110117
// Inputs should still be present.
111118
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "cache_repo", deps.CacheRepo),
112119
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "extra_env.FOO", "bar"),
113120
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "git_url", deps.Repo.URL),
114-
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "exists", "false"),
115-
resource.TestCheckResourceAttr("envbuilder_cached_image.test", "image", deps.BuilderImage),
116121
// Should be empty
117122
resource.TestCheckNoResourceAttr("envbuilder_cached_image.test", "git_username"),
118123
resource.TestCheckNoResourceAttr("envbuilder_cached_image.test", "git_password"),
119124
resource.TestCheckNoResourceAttr("envbuilder_cached_image.test", "cache_ttl_days"),
120-
// Computed values should be empty.
121-
resource.TestCheckNoResourceAttr("envbuilder_cached_image.test", "id"),
122-
resource.TestCheckResourceAttrSet("envbuilder_cached_image.test", "env.0"),
123125
),
124126
},
125127
},

0 commit comments

Comments
 (0)