use common entry point for integration tests; refactor cmd pkg (#86)

This commit is contained in:
Alex Goodman
2020-07-17 15:16:33 -04:00
committed by GitHub
parent b5a353349f
commit 9e285fd0e2
15 changed files with 234 additions and 252 deletions
+59
View File
@@ -0,0 +1,59 @@
package scope
import "strings"
// Potentially consider moving this out into a generic package that parses user input.
// Aside from scope, this is the 2nd package that looks at a string to parse the input
// and return an Option type.
const (
// nolint:varcheck,deadcode
unknownProtocol protocolType = iota
imageProtocol
directoryProtocol
)
var protocolStr = []string{
"UnknownProtocol",
"Image",
"Directory",
}
type protocolType int
type protocol struct {
Type protocolType
Value string
}
func newProtocol(userStr string) protocol {
candidates := strings.Split(userStr, "://")
switch len(candidates) {
case 2:
if strings.HasPrefix(userStr, "dir://") {
return protocol{
Type: directoryProtocol,
Value: strings.TrimPrefix(userStr, "dir://"),
}
}
// default to an Image for anything else since stereoscope can handle this
return protocol{
Type: imageProtocol,
Value: userStr,
}
default:
return protocol{
Type: imageProtocol,
Value: userStr,
}
}
}
func (o protocolType) String() string {
if int(o) >= len(protocolStr) || o < 0 {
return protocolStr[0]
}
return protocolStr[o]
}
+43
View File
@@ -0,0 +1,43 @@
package scope
import "testing"
func TestNewProtocol(t *testing.T) {
testCases := []struct {
desc string
input string
expType protocolType
expValue string
}{
{
desc: "directory protocol",
input: "dir:///opt/",
expType: directoryProtocol,
expValue: "/opt/",
},
{
desc: "unknown protocol",
input: "s4:///opt/",
expType: imageProtocol,
expValue: "s4:///opt/",
},
{
desc: "docker protocol",
input: "docker://ubuntu:20.04",
expType: imageProtocol,
expValue: "docker://ubuntu:20.04",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
p := newProtocol(test.input)
if p.Type != test.expType {
t.Errorf("mismatched type in protocol: '%v' != '%v'", p.Type, test.expType)
}
if p.Value != test.expValue {
t.Errorf("mismatched protocol value: '%s' != '%s'", p.Value, test.expValue)
}
})
}
}
+38
View File
@@ -3,7 +3,10 @@ package scope
import (
"fmt"
"github.com/anchore/stereoscope"
"github.com/anchore/imgbom/imgbom/scope/resolvers"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/image"
)
@@ -23,6 +26,41 @@ type Scope struct {
DirSrc DirSource
}
// NewScope produces a Scope based on userInput like dir:// or image:tag
func NewScope(userInput string, o Option) (Scope, func(), error) {
protocol := newProtocol(userInput)
switch protocol.Type {
case directoryProtocol:
// populate the scope object for dir
s, err := NewScopeFromDir(protocol.Value, o)
if err != nil {
return Scope{}, func() {}, fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err)
}
return s, func() {}, nil
case imageProtocol:
log.Infof("Fetching image '%s'", userInput)
img, err := stereoscope.GetImage(userInput)
cleanup := func() {
stereoscope.Cleanup()
}
if err != nil || img == nil {
return Scope{}, cleanup, fmt.Errorf("could not fetch image '%s': %w", userInput, err)
}
s, err := NewScopeFromImage(img, o)
if err != nil {
return Scope{}, cleanup, fmt.Errorf("could not populate scope with image: %w", err)
}
return s, cleanup, nil
default:
return Scope{}, func() {}, fmt.Errorf("unable to process input for scanning: '%s'", userInput)
}
}
func NewScopeFromDir(path string, option Option) (Scope, error) {
return Scope{
Option: option,