Skip to content

Commit

Permalink
fix: warning message is printed to stdout by CLI (#1650)
Browse files Browse the repository at this point in the history
Signed-off-by: Susan Shi <[email protected]>
  • Loading branch information
susanshi authored Aug 1, 2024
1 parent 17f829a commit 7294999
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 20 deletions.
83 changes: 83 additions & 0 deletions cmd/ratify/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
8 changes: 4 additions & 4 deletions cmd/ratify/cmd/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions cmd/ratify/cmd/referrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions cmd/ratify/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd
import (
"context"
"errors"
"fmt"

"github.com/ratify-project/ratify/config"
"github.com/ratify-project/ratify/internal/constants"
Expand All @@ -36,6 +35,10 @@ const (
verifyUse = "verify"
)

var logOpt = logger.Option{
ComponentType: logger.CommandLine,
}

type verifyCmdOptions struct {
configFilePath string
subject string
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7294999

Please sign in to comment.