Skip to content

Commit

Permalink
check for images property if cacheFrom is present (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzyfresh authored Mar 20, 2023
1 parent f35708e commit fde2cb9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
16 changes: 11 additions & 5 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context,
if err != nil {
return "", nil, err
}
cache := marshalCachedImages(img, inputs["build"])
cache, err := marshalCachedImages(inputs["build"])
if err != nil {
return "", nil, err
}

build.CachedImages = cache
img.Build = build
Expand Down Expand Up @@ -390,25 +393,28 @@ func marshalBuildAndApplyDefaults(b resource.PropertyValue) (Build, error) {
return build, nil
}

func marshalCachedImages(img Image, b resource.PropertyValue) []string {
func marshalCachedImages(b resource.PropertyValue) ([]string, error) {
var cacheImages []string
if b.IsNull() {
return cacheImages
return cacheImages, nil
}
c := b.ObjectValue()["cacheFrom"]

if c.IsNull() {
return cacheImages
return cacheImages, nil
}

// if we specify a list of stages, then we only pull those
cacheFrom := c.ObjectValue()
if cacheFrom["images"].IsNull() {
return cacheImages, fmt.Errorf("if you want to use cacheFrom, you must have `images` set")
}
stages := cacheFrom["images"].ArrayValue()
for _, img := range stages {
stage := img.StringValue()
cacheImages = append(cacheImages, stage)
}
return cacheImages
return cacheImages, nil
}

func marshalRegistry(r resource.PropertyValue) Registry {
Expand Down
47 changes: 16 additions & 31 deletions provider/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,6 @@ func TestMarshalArgs(t *testing.T) {
func TestMarshalCachedImages(t *testing.T) {
t.Run("Test Cached Images", func(t *testing.T) {
expected := []string{"apple", "banana", "cherry"}
imgInput := Image{
Name: "unicornsareawesome",
SkipPush: false,
Registry: Registry{
Server: "https://index.docker.io/v1/",
Username: "pulumipus",
Password: "supersecret",
},
}
buildInput := resource.NewObjectProperty(resource.PropertyMap{
"dockerfile": resource.NewStringProperty("TheLastUnicorn"),
"context": resource.NewStringProperty("/twilight/sparkle/bin"),
Expand All @@ -227,45 +218,39 @@ func TestMarshalCachedImages(t *testing.T) {
}),
}),
})

actual := marshalCachedImages(imgInput, buildInput)
actual, err := marshalCachedImages(buildInput)
assert.NoError(t, err)
assert.Equal(t, expected, actual)

})
t.Run("Test Cached Images No Build Input Returns Nil", func(t *testing.T) {
expected := []string(nil)
imgInput := Image{
Name: "unicornsareawesome",
SkipPush: false,
Registry: Registry{
Server: "https://index.docker.io/v1/",
Username: "pulumipus",
Password: "supersecret",
},
}
buildInput := resource.NewObjectProperty(resource.PropertyMap{})
actual := marshalCachedImages(imgInput, buildInput)
actual, err := marshalCachedImages(buildInput)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})

t.Run("Test Cached Images No cacheFrom Input Returns Nil", func(t *testing.T) {
expected := []string(nil)
imgInput := Image{
Name: "unicornsareawesome",
SkipPush: false,
Registry: Registry{
Server: "https://index.docker.io/v1/",
Username: "pulumipus",
Password: "supersecret",
},
}
buildInput := resource.NewObjectProperty(resource.PropertyMap{
"dockerfile": resource.NewStringProperty("TheLastUnicorn"),
"context": resource.NewStringProperty("/twilight/sparkle/bin"),
})
actual := marshalCachedImages(imgInput, buildInput)
actual, err := marshalCachedImages(buildInput)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("Test Cached Images No images Input Returns Nil and error", func(t *testing.T) {
buildInput := resource.NewObjectProperty(resource.PropertyMap{
"dockerfile": resource.NewStringProperty("TheLastUnicorn"),
"context": resource.NewStringProperty("/twilight/sparkle/bin"),
"cacheFrom": resource.NewObjectProperty(resource.PropertyMap{}),
})
actual, err := marshalCachedImages(buildInput)
assert.Error(t, err)
assert.Nil(t, actual)
})
}

func TestMarshalBuilder(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"io"
"io/fs"
"os"
Expand Down Expand Up @@ -189,6 +190,11 @@ func (p *dockerNativeProvider) Check(ctx context.Context, req *rpc.CheckRequest)
return nil, err
}
}

if _, err := marshalCachedImages(inputs["build"]); err != nil {
err = p.host.Log(ctx, diag.Error, urn, msg)
return nil, err
}
}

inputStruct, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{
Expand Down

0 comments on commit fde2cb9

Please sign in to comment.