|
| 1 | +package validate |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + rspec "github.com/opencontainers/runtime-spec/specs-go" |
| 11 | +) |
| 12 | + |
| 13 | +func checkErrors(t *testing.T, title string, msgs []string, valid bool) { |
| 14 | + if valid && len(msgs) > 0 { |
| 15 | + t.Fatalf("%s: expected not to get error, but get %d errors:\n%s", title, len(msgs), strings.Join(msgs, "\n")) |
| 16 | + } else if !valid && len(msgs) == 0 { |
| 17 | + t.Fatalf("%s: expected to get error, but actually not", title) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestCheckRootfsPath(t *testing.T) { |
| 22 | + tmpBundle, err := ioutil.TempDir("", "oci-check-rootfspath") |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to create a TempDir in 'CheckRootfsPath'") |
| 25 | + } |
| 26 | + defer os.RemoveAll(tmpBundle) |
| 27 | + |
| 28 | + rootfsDir := "rootfs" |
| 29 | + rootfsNonDir := "rootfsfile" |
| 30 | + rootfsNonExists := "rootfsnil" |
| 31 | + if err := os.MkdirAll(filepath.Join(tmpBundle, rootfsDir), 0700); err != nil { |
| 32 | + t.Fatalf("Failed to create a rootfs directory in 'CheckRootfsPath'") |
| 33 | + } |
| 34 | + if _, err := os.Create(filepath.Join(tmpBundle, rootfsNonDir)); err != nil { |
| 35 | + t.Fatalf("Failed to create a non-directory rootfs in 'CheckRootfsPath'") |
| 36 | + } |
| 37 | + |
| 38 | + cases := []struct { |
| 39 | + val string |
| 40 | + expected bool |
| 41 | + }{ |
| 42 | + {rootfsDir, true}, |
| 43 | + {rootfsNonDir, false}, |
| 44 | + {rootfsNonExists, false}, |
| 45 | + {filepath.Join(tmpBundle, rootfsDir), true}, |
| 46 | + {filepath.Join(tmpBundle, rootfsNonDir), false}, |
| 47 | + {filepath.Join(tmpBundle, rootfsNonExists), false}, |
| 48 | + } |
| 49 | + for _, c := range cases { |
| 50 | + v := NewValidator(&rspec.Spec{Root: rspec.Root{Path: c.val}}, tmpBundle, false) |
| 51 | + checkErrors(t, "CheckRootfsPath "+c.val, v.CheckRootfsPath(), c.expected) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestCheckSemVer(t *testing.T) { |
| 56 | + cases := []struct { |
| 57 | + val string |
| 58 | + expected bool |
| 59 | + }{ |
| 60 | + {rspec.Version, true}, |
| 61 | + //FIXME: validate currently only handles rpsec.Version |
| 62 | + {"0.0.1", false}, |
| 63 | + {"invalid", false}, |
| 64 | + } |
| 65 | + |
| 66 | + for _, c := range cases { |
| 67 | + v := NewValidator(&rspec.Spec{Version: c.val}, "", false) |
| 68 | + checkErrors(t, "CheckSemVer "+c.val, v.CheckSemVer(), c.expected) |
| 69 | + } |
| 70 | +} |
0 commit comments