|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "jmm/pkg/artifact" |
| 6 | + "jmm/pkg/lib/constants" |
| 7 | + internal "jmm/pkg/lib/testing" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/opencontainers/go-digest" |
| 11 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestListModels(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + manifests map[digest.Digest]ocispec.Manifest |
| 19 | + configs map[digest.Digest]artifact.JozuFile |
| 20 | + index *ocispec.Index |
| 21 | + expectedOutputRegexps []string |
| 22 | + expectErrRegexp string |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "Cannot read index.json", |
| 26 | + index: nil, |
| 27 | + expectErrRegexp: "artifact not found", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Cannot find manifest from index.json", |
| 31 | + index: &ocispec.Index{ |
| 32 | + Manifests: []ocispec.Descriptor{ |
| 33 | + ManifestDesc("manifestA", true), |
| 34 | + ManifestDesc("manifestNotFound", true), |
| 35 | + }, |
| 36 | + }, |
| 37 | + manifests: map[digest.Digest]ocispec.Manifest{ |
| 38 | + "manifestA": Manifest("configA", "layerA"), |
| 39 | + "manifestB": Manifest("configB", "layerB"), |
| 40 | + }, |
| 41 | + configs: map[digest.Digest]artifact.JozuFile{ |
| 42 | + "configA": Config("maintainerA", "formatA"), |
| 43 | + "configB": Config("maintainerB", "formatB"), |
| 44 | + }, |
| 45 | + expectErrRegexp: "failed to read manifest manifestNotFound.*", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Cannot find config in manifest", |
| 49 | + index: &ocispec.Index{ |
| 50 | + Manifests: []ocispec.Descriptor{ |
| 51 | + ManifestDesc("manifestA", true), |
| 52 | + ManifestDesc("manifestB", true), |
| 53 | + }, |
| 54 | + }, |
| 55 | + manifests: map[digest.Digest]ocispec.Manifest{ |
| 56 | + "manifestA": Manifest("configA", "layerA"), |
| 57 | + "manifestB": Manifest("configNotFound", "layerB"), |
| 58 | + }, |
| 59 | + configs: map[digest.Digest]artifact.JozuFile{ |
| 60 | + "configA": Config("maintainerA", "formatA"), |
| 61 | + "configB": Config("maintainerB", "formatB"), |
| 62 | + }, |
| 63 | + expectErrRegexp: "failed to read config.*", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Prints summary of for each manifest line (layers are 1024 bytes)", |
| 67 | + index: &ocispec.Index{ |
| 68 | + Manifests: []ocispec.Descriptor{ |
| 69 | + ManifestDesc("manifestA", true), |
| 70 | + ManifestDesc("manifestB", true), |
| 71 | + }, |
| 72 | + }, |
| 73 | + manifests: map[digest.Digest]ocispec.Manifest{ |
| 74 | + "manifestA": Manifest("configA", "layerA"), |
| 75 | + "manifestB": Manifest("configB", "layerB1", "layerB2", "layerB3"), |
| 76 | + }, |
| 77 | + configs: map[digest.Digest]artifact.JozuFile{ |
| 78 | + "configA": Config("maintainerA", "formatA"), |
| 79 | + "configB": Config("maintainerB", "formatB"), |
| 80 | + }, |
| 81 | + expectedOutputRegexps: []string{ |
| 82 | + "manifestA.*maintainerA.*formatA.*1.0 KiB", |
| 83 | + "manifestB.*maintainerB.*formatB.*3.0 KiB", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + testStore := &internal.TestStore{ |
| 90 | + Manifests: tt.manifests, |
| 91 | + Configs: tt.configs, |
| 92 | + Index: tt.index, |
| 93 | + } |
| 94 | + summary, err := listModels(testStore) |
| 95 | + if tt.expectErrRegexp != "" { |
| 96 | + // Should be error |
| 97 | + assert.Empty(t, summary, "Should not output summary on error") |
| 98 | + if assert.Error(t, err, "Should return an error") { |
| 99 | + return |
| 100 | + } |
| 101 | + assert.Regexp(t, tt.expectErrRegexp, err.Error()) |
| 102 | + } else { |
| 103 | + if !assert.NoError(t, err, "Should not return an error") { |
| 104 | + return |
| 105 | + } |
| 106 | + for _, line := range tt.expectedOutputRegexps { |
| 107 | + // Assert all lines in expected output are somewhere in the summary |
| 108 | + assert.Regexp(t, line, summary) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestFormatBytes(t *testing.T) { |
| 116 | + tests := []struct { |
| 117 | + input int64 |
| 118 | + output string |
| 119 | + }{ |
| 120 | + {input: 0, output: "0 B"}, |
| 121 | + {input: 500, output: "500 B"}, |
| 122 | + {input: 1<<10 - 1, output: "1023 B"}, |
| 123 | + {input: 1 << 10, output: "1.0 KiB"}, |
| 124 | + {input: 4.5 * (1 << 10), output: "4.5 KiB"}, |
| 125 | + {input: 1<<20 - 1, output: "1023.9 KiB"}, |
| 126 | + {input: 1 << 20, output: "1.0 MiB"}, |
| 127 | + {input: 6.5 * (1 << 20), output: "6.5 MiB"}, |
| 128 | + {input: 1<<30 - 1, output: "1023.9 MiB"}, |
| 129 | + {input: 1 << 30, output: "1.0 GiB"}, |
| 130 | + {input: 1 << 40, output: "1.0 TiB"}, |
| 131 | + {input: 1 << 50, output: "1.0 PiB"}, |
| 132 | + {input: 500 * (1 << 50), output: "500.0 PiB"}, |
| 133 | + {input: 1 << 60, output: "1024.0 PiB"}, |
| 134 | + } |
| 135 | + for idx, tt := range tests { |
| 136 | + t.Run(fmt.Sprintf("test %d", idx), func(t *testing.T) { |
| 137 | + output := formatBytes(tt.input) |
| 138 | + assert.Equalf(t, tt.output, output, "Should convert %d to %s", tt.input, tt.output) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func Manifest(configDigest string, layerDigests ...string) ocispec.Manifest { |
| 144 | + manifest := ocispec.Manifest{ |
| 145 | + Config: ocispec.Descriptor{ |
| 146 | + MediaType: constants.ModelConfigMediaType, |
| 147 | + Digest: digest.Digest(configDigest), |
| 148 | + }, |
| 149 | + } |
| 150 | + for _, layerDigest := range layerDigests { |
| 151 | + manifest.Layers = append(manifest.Layers, ocispec.Descriptor{ |
| 152 | + MediaType: constants.ModelLayerMediaType, |
| 153 | + Digest: digest.Digest(layerDigest), |
| 154 | + Size: 1024, |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + return manifest |
| 159 | +} |
| 160 | + |
| 161 | +func Config(maintainer, format string) artifact.JozuFile { |
| 162 | + config := artifact.JozuFile{ |
| 163 | + Maintainer: maintainer, |
| 164 | + ModelFormat: format, |
| 165 | + } |
| 166 | + |
| 167 | + return config |
| 168 | +} |
| 169 | + |
| 170 | +func ManifestDesc(digestStr string, valid bool) ocispec.Descriptor { |
| 171 | + size := internal.ValidSize |
| 172 | + if !valid { |
| 173 | + size = internal.InvalidSize |
| 174 | + } |
| 175 | + |
| 176 | + return ocispec.Descriptor{ |
| 177 | + Digest: digest.Digest(digestStr), |
| 178 | + MediaType: ocispec.MediaTypeImageManifest, |
| 179 | + Size: size, |
| 180 | + } |
| 181 | +} |
0 commit comments