image_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package image
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "strings"
  6. "testing"
  7. )
  8. const sampleImageJSON = `{
  9. "architecture": "amd64",
  10. "os": "linux",
  11. "config": {},
  12. "rootfs": {
  13. "type": "layers",
  14. "diff_ids": []
  15. }
  16. }`
  17. func TestJSON(t *testing.T) {
  18. img, err := NewFromJSON([]byte(sampleImageJSON))
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. rawJSON := img.RawJSON()
  23. if string(rawJSON) != sampleImageJSON {
  24. t.Fatalf("Raw JSON of config didn't match: expected %+v, got %v", sampleImageJSON, rawJSON)
  25. }
  26. }
  27. func TestInvalidJSON(t *testing.T) {
  28. _, err := NewFromJSON([]byte("{}"))
  29. if err == nil {
  30. t.Fatal("Expected JSON parse error")
  31. }
  32. }
  33. func TestMarshalKeyOrder(t *testing.T) {
  34. b, err := json.Marshal(&Image{
  35. V1Image: V1Image{
  36. Comment: "a",
  37. Author: "b",
  38. Architecture: "c",
  39. },
  40. })
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. expectedOrder := []string{"architecture", "author", "comment"}
  45. var indexes []int
  46. for _, k := range expectedOrder {
  47. indexes = append(indexes, strings.Index(string(b), k))
  48. }
  49. if !sort.IntsAreSorted(indexes) {
  50. t.Fatal("invalid key order in JSON: ", string(b))
  51. }
  52. }