diff --git a/cmd/ratify/cmd/cmd_test.go b/cmd/ratify/cmd/cmd_test.go new file mode 100644 index 000000000..49149dcf5 --- /dev/null +++ b/cmd/ratify/cmd/cmd_test.go @@ -0,0 +1,83 @@ +/* +Copyright The Ratify Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "strings" + "testing" +) + +const ( + configFilePath = "../../../config/config.json" + subject = "localhost:5000/net-monitor:v1" + storeName = "oras" + digest = "sha256:17490f904cf278d4314a1ccba407fc8fd00fb45303589b8cc7f5174ac35554f4" +) + +func TestVerify(t *testing.T) { + err := verify((verifyCmdOptions{ + subject: subject, + artifactTypes: []string{""}, + configFilePath: configFilePath, + })) + + // TODO: make ratify cli more unit testable + // unit test should not have dependency for real image + if !strings.Contains(err.Error(), "plugin not found") { + t.Errorf("error expected") + } +} + +func TestDiscover(t *testing.T) { + err := discover((discoverCmdOptions{ + subject: subject, + artifactTypes: []string{""}, + configFilePath: configFilePath, + })) + + // TODO: make ratify cli more unit testable + // unit test should not need to resolve real image + if !strings.Contains(err.Error(), "referrer store failure") { + t.Errorf("error expected") + } +} + +func TestShowRefManifest(t *testing.T) { + err := showRefManifest((referrerCmdOptions{ + subject: subject, + configFilePath: configFilePath, + storeName: storeName, + digest: digest, + })) + + // TODO: make ratify cli more unit testable + // unit test should not need to resolve real image + if !strings.Contains(err.Error(), "failed to resolve subject descriptor") { + t.Errorf("error expected") + } + + // validate show blob returns error + err = showBlob((referrerCmdOptions{ + subject: subject, + configFilePath: configFilePath, + storeName: storeName, + digest: "invalid-digest", + })) + + if !strings.Contains(err.Error(), "the digest of the subject is invalid") { + t.Errorf("error expected") + } +} diff --git a/cmd/ratify/cmd/discover.go b/cmd/ratify/cmd/discover.go index 66d9d0921..858f12b52 100644 --- a/cmd/ratify/cmd/discover.go +++ b/cmd/ratify/cmd/discover.go @@ -96,10 +96,6 @@ func discover(opts discoverCmdOptions) error { return err } - if subRef.Digest == "" { - fmt.Println(taggedReferenceWarning) - } - cf, err := config.Load(opts.configFilePath) if err != nil { return err @@ -109,6 +105,10 @@ func discover(opts discoverCmdOptions) error { return err } + if subRef.Digest == "" { + logger.GetLogger(context.Background(), logOpt).Warn(taggedReferenceWarning) + } + rootImage := treeprint.NewWithRoot(subRef.String()) stores, err := sf.CreateStoresFromConfig(cf.StoresConfig, config.GetDefaultPluginPath()) diff --git a/cmd/ratify/cmd/referrer.go b/cmd/ratify/cmd/referrer.go index 67ebe2d9a..8921c5ede 100644 --- a/cmd/ratify/cmd/referrer.go +++ b/cmd/ratify/cmd/referrer.go @@ -97,7 +97,7 @@ func NewCmdShowRefManifest(argv ...string) *cobra.Command { cmd := &cobra.Command{ Use: "show-manifest [OPTIONS]", - Short: "show rference manifest at a digest", + Short: "show reference manifest at a digest", Example: eg, Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { @@ -184,10 +184,6 @@ func showRefManifest(opts referrerCmdOptions) error { return err } - if subRef.Digest == "" { - fmt.Println(taggedReferenceWarning) - } - digest, err := utils.ParseDigest(opts.digest) if err != nil { return err @@ -198,6 +194,10 @@ func showRefManifest(opts referrerCmdOptions) error { return err } + if subRef.Digest == "" { + logger.GetLogger(context.Background(), logOpt).Warn(taggedReferenceWarning) + } + stores, err := sf.CreateStoresFromConfig(cf.StoresConfig, config.GetDefaultPluginPath()) if err != nil { diff --git a/cmd/ratify/cmd/verify.go b/cmd/ratify/cmd/verify.go index 1e745e9c6..c189fa68b 100644 --- a/cmd/ratify/cmd/verify.go +++ b/cmd/ratify/cmd/verify.go @@ -18,7 +18,6 @@ package cmd import ( "context" "errors" - "fmt" "github.com/ratify-project/ratify/config" "github.com/ratify-project/ratify/internal/constants" @@ -36,6 +35,10 @@ const ( verifyUse = "verify" ) +var logOpt = logger.Option{ + ComponentType: logger.CommandLine, +} + type verifyCmdOptions struct { configFilePath string subject string @@ -65,12 +68,6 @@ func NewCmdVerify(_ ...string) *cobra.Command { return cmd } -func TestVerify(subject string) { - _ = verify((verifyCmdOptions{ - subject: subject, - })) -} - func verify(opts verifyCmdOptions) error { if opts.subject == "" { return errors.New("subject parameter is required") @@ -81,10 +78,6 @@ func verify(opts verifyCmdOptions) error { return err } - if subRef.Digest == "" { - fmt.Println(taggedReferenceWarning) - } - cf, err := config.Load(opts.configFilePath) if err != nil { return err @@ -94,6 +87,10 @@ func verify(opts verifyCmdOptions) error { return err } + if subRef.Digest == "" { + logger.GetLogger(context.Background(), logOpt).Warn(taggedReferenceWarning) + } + stores, err := sf.CreateStoresFromConfig(cf.StoresConfig, config.GetDefaultPluginPath()) if err != nil { diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 6721cd65e..ff050cef3 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -61,6 +61,8 @@ const ( Executor componentType = "executor" // Server is the component type for the Ratify http server. Server componentType = "server" + // CommandLine is the component type for the Ratify command line. + CommandLine componentType = "commandLine" // ReferrerStore is the component type for the referrer store. ReferrerStore componentType = "referrerStore" // Cache is the component type for the cache.