image_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package image
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. const sampleImageJSON = `{
  11. "architecture": "amd64",
  12. "os": "linux",
  13. "config": {},
  14. "rootfs": {
  15. "type": "layers",
  16. "diff_ids": []
  17. }
  18. }`
  19. func TestNewFromJSON(t *testing.T) {
  20. img, err := NewFromJSON([]byte(sampleImageJSON))
  21. require.NoError(t, err)
  22. assert.Equal(t, sampleImageJSON, string(img.RawJSON()))
  23. }
  24. func TestNewFromJSONWithInvalidJSON(t *testing.T) {
  25. _, err := NewFromJSON([]byte("{}"))
  26. assert.EqualError(t, err, "invalid image JSON, no RootFS key")
  27. }
  28. func TestMarshalKeyOrder(t *testing.T) {
  29. b, err := json.Marshal(&Image{
  30. V1Image: V1Image{
  31. Comment: "a",
  32. Author: "b",
  33. Architecture: "c",
  34. },
  35. })
  36. assert.NoError(t, err)
  37. expectedOrder := []string{"architecture", "author", "comment"}
  38. var indexes []int
  39. for _, k := range expectedOrder {
  40. indexes = append(indexes, strings.Index(string(b), k))
  41. }
  42. if !sort.IntsAreSorted(indexes) {
  43. t.Fatal("invalid key order in JSON: ", string(b))
  44. }
  45. }