Skip to content

Commit

Permalink
fix some tests using .Image and create new test
Browse files Browse the repository at this point in the history
Signed-off-by: gauron99 <fridrich.david19@gmail.com>
  • Loading branch information
gauron99 committed Nov 6, 2023
1 parent 4ac3e54 commit 6ef0d51
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 89 deletions.
1 change: 1 addition & 0 deletions cmd/.func/built
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b61d586d291fdb296d942a12d58159099e20dd590c77ed40e85d83178310d719
68 changes: 29 additions & 39 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
f.Deploy.Image = f.Build.Image
// write .func/built-name as running metadata which is not persisted in yaml
if err = f.WriteBuiltImageOnChange(cfg.Verbose); err != nil {
return
}
}

Expand Down Expand Up @@ -540,18 +541,6 @@ func (c deployConfig) Configure(f fn.Function) (fn.Function, error) {
f.Build.PVCSize = c.PVCSize
}

// TODO: gauron99 parse --image if provided:
// image with digest == i want to skip everything and just deploy
// image without digest == if building as well - use THIS image name

// // ImageDigest
// // Parsed off f.Build.Image if provided. Deploying adds the ability to specify a
// // digest on the associated image (not available on build as nonsensical).
// hasDigest, err := hasDigest(f.Build.Image)
// if err != nil {
// return f, err
// }

// Envs
// Preprocesses any Envs provided (which may include removals) into a final
// set
Expand Down Expand Up @@ -700,36 +689,11 @@ func (c deployConfig) Validate(cmd *cobra.Command) (err error) {
return
}

// hasDigest returns the image digest from a full image string if it exists,
// and includes basic validation that a provided digest is correctly formatted.
func hasDigest(v string) (hasDigest bool, err error) {
var digest string
vv := strings.Split(v, "@")
if len(vv) < 2 {
return // has no digest
} else if len(vv) > 2 {
err = fmt.Errorf("image '%v' contains an invalid digest (extra '@')", v)
return
}
digest = vv[1]

if !strings.HasPrefix(digest, "sha256:") {
err = fmt.Errorf("image digest '%s' requires 'sha256:' prefix", digest)
return
}

if len(digest[7:]) != 64 {
err = fmt.Errorf("image digest '%v' has an invalid sha256 hash length of %v when it should be 64", digest, len(digest[7:]))
}

hasDigest = true
return
}

// printDeployMessages to the output. Non-error deployment messages.
func printDeployMessages(out io.Writer, cfg deployConfig, f fn.Function) {
// TODO: gauron check
digest, err := hasDigest(cfg.Image)
if err != nil && digest {
if err == nil && digest {
fmt.Fprintf(out, "Deploying image '%v', which has a digest. Build and push are disabled.\n", cfg.Image)
}

Expand Down Expand Up @@ -776,3 +740,29 @@ func printDeployMessages(out io.Writer, cfg deployConfig, f fn.Function) {
fmt.Fprintf(out, "Warning: git settings are only applicable when running with --remote. Local source code will be used.")
}
}

// hasDigest returns the image digest from a full image string if it exists,
// and includes basic validation that a provided digest is correctly formatted.
func hasDigest(v string) (hasDigest bool, err error) {
var digest string
vv := strings.Split(v, "@")
if len(vv) < 2 {
return // has no digest
} else if len(vv) > 2 {
err = fmt.Errorf("image '%v' contains an invalid digest (extra '@')", v)
return
}
digest = vv[1]

if !strings.HasPrefix(digest, "sha256:") {
err = fmt.Errorf("image digest '%s' requires 'sha256:' prefix", digest)
return
}

if len(digest[7:]) != 64 {
err = fmt.Errorf("image digest '%v' has an invalid sha256 hash length of %v when it should be 64", digest, len(digest[7:]))
}

hasDigest = true
return
}
24 changes: 15 additions & 9 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func testConfigApplied(cmdFn commandConstructor, t *testing.T) {
if f, err = fn.NewFunction(root); err != nil {
t.Fatal(err)
}
if f.Image != "registry.example.com/charlie/f:latest" {
t.Fatalf("expected image 'registry.example.com/charlie/f:latest' got '%v'", f.Image)
if f.Build.Image != "registry.example.com/charlie/f:latest" {
t.Fatalf("expected image 'registry.example.com/charlie/f:latest' got '%v'", f.Build.Image)
}

// Ensure environment variables loaded: Push
Expand Down Expand Up @@ -1124,7 +1124,9 @@ func testRegistry(cmdFn commandConstructor, t *testing.T) {
name: "registry member mismatch",
f: fn.Function{
Registry: "registry.example.com/alice",
Image: "registry.example.com/bob/f:latest",
Build: fn.BuildSpec{
Image: "registry.example.com/bob/f:latest",
},
},
args: []string{},
expectedRegistry: "registry.example.com/alice",
Expand All @@ -1137,7 +1139,9 @@ func testRegistry(cmdFn commandConstructor, t *testing.T) {
name: "registry flag updates",
f: fn.Function{
Registry: "registry.example.com/alice",
Image: "registry.example.com/bob/f:latest",
Build: fn.BuildSpec{
Image: "registry.example.com/bob/f:latest",
},
},
args: []string{"--registry=registry.example.com/charlie"},
expectedRegistry: "registry.example.com/charlie",
Expand All @@ -1150,7 +1154,9 @@ func testRegistry(cmdFn commandConstructor, t *testing.T) {
name: "image flag overrides",
f: fn.Function{
Registry: "registry.example.com/alice",
Image: "registry.example.com/bob/f:latest",
Build: fn.BuildSpec{
Image: "registry.example.com/bob/f:latest",
},
},
args: []string{"--image=registry.example.com/charlie/f:latest"},
expectedRegistry: "registry.example.com/alice", // not updated
Expand Down Expand Up @@ -1178,8 +1184,8 @@ func testRegistry(cmdFn commandConstructor, t *testing.T) {
if f.Registry != test.expectedRegistry {
t.Fatalf("expected registry '%v', got '%v'", test.expectedRegistry, f.Registry)
}
if f.Image != test.expectedImage {
t.Fatalf("expected image '%v', got '%v'", test.expectedImage, f.Image)
if f.Build.Image != test.expectedImage {
t.Fatalf("expected image '%v', got '%v'", test.expectedImage, f.Build.Image)
}
})
}
Expand Down Expand Up @@ -1220,8 +1226,8 @@ func testRegistryLoads(cmdFn commandConstructor, t *testing.T) {
}

expected := "example.com/alice/my-func:latest"
if f.Image != expected {
t.Fatalf("expected image name '%v'. got %v", expected, f.Image)
if f.Build.Image != expected {
t.Fatalf("expected image name '%v'. got %v", expected, f.Build.Image)
}
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/my_super_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"testing"

fn "knative.dev/func/pkg/functions"
)

func TestDebugger(t *testing.T) {
// CD into a new tmp directory
root := fromTempDirectory(t)

// Create a new default function in Go
if _, err := fn.New().Init(fn.Function{Runtime: "go", Root: root}); err != nil {
t.Fatal(err)
}

// cmd := NewDeployCmd(NewTestClient(fn.WithRegistry("docker.io/4114gauron3268")))
cmd := NewBuildCmd(NewTestClient(fn.WithRegistry("docker.io/4114gauron3268")))
// cmd.SetArgs([]string{"--builder=pack", "--verbose"}) // Or whatever you're testing
cmd.SetArgs([]string{"--verbose"}) // Or whatever you're testing

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
}
10 changes: 7 additions & 3 deletions pkg/docker/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func TestDaemonPush(t *testing.T) {
)

f := fn.Function{
Image: functionImageLocal,
Build: fn.BuildSpec{
Image: functionImageLocal,
},
}

digest, err := pusher.Push(ctx, f)
Expand Down Expand Up @@ -196,15 +198,17 @@ func TestNonDaemonPush(t *testing.T) {
)

f := fn.Function{
Image: functionImageRemote,
Build: fn.BuildSpec{
Image: functionImageRemote,
},
}

actualDigest, err := pusher.Push(ctx, f)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(imagesPassedToMock, []string{f.Image}) {
if !reflect.DeepEqual(imagesPassedToMock, []string{f.Build.Image}) {
t.Errorf("Bad image name passed to the Docker API Client: %q.", imagesPassedToMock)
}

Expand Down
19 changes: 14 additions & 5 deletions pkg/functions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,20 +759,29 @@ func (c *Client) Deploy(ctx context.Context, f Function, opts ...DeployOption) (
// Returned function contains applicable registry and deployed image name.
func (c *Client) RunPipeline(ctx context.Context, f Function) (Function, error) {
var err error

// Default function registry to the client's global registry
if f.Registry == "" {
f.Registry = c.registry
}

// If no image name has been yet defined (not yet built/deployed), calculate.
// Image name is stored on the function for later use by deploy, etc.
if f.Deploy.Image == "" {
if f.Deploy.Image, err = f.ImageName(); err != nil {
// If no image name has been yet defined (not yet built/deployed), calculate
// unless f.Image was specified - user defined image name to be used
// Image name is stored on the function for later use by deploy.
// TODO: gauron99-should this be stored somewhere else as well? like
// .Build.Image since the Function is going to be built(&deployed) on cluster
// under the f.Deploy.Image name. In scenario Init -> Deploy with remote,
// f.Build.Image is not gonna be populated
if f.Image != "" {
// if user specified an image, use it
f.Deploy.Image = f.Image
} else if f.Deploy.Image == "" {
f.Deploy.Image, err = f.ImageName()
if err != nil {
return f, err
}
}

fmt.Printf("-- deploy: %s| f.Image: %s\n", f.Deploy.Image, f.Image)
// Build and deploy function using Pipeline
if _, err := c.pipelinesProvider.Run(ctx, f); err != nil {
return f, fmt.Errorf("failed to run pipeline: %w", err)
Expand Down
Loading

0 comments on commit 6ef0d51

Please sign in to comment.