validate_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package config
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  7. "github.com/rancher/os/util"
  8. )
  9. func testValidate(t *testing.T, cfg []byte, contains string) {
  10. fmt.Printf("Testing %s, contains %s", string(cfg), contains)
  11. validationErrors, err := ValidateBytes(cfg)
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. if contains == "" && len(validationErrors.Errors()) != 0 {
  16. t.Fail()
  17. }
  18. if !strings.Contains(fmt.Sprint(validationErrors.Errors()), contains) {
  19. t.Fail()
  20. }
  21. }
  22. func TestValidate(t *testing.T) {
  23. testValidate(t, []byte("{}"), "")
  24. testValidate(t, []byte(`rancher:
  25. log: true`), "")
  26. testValidate(t, []byte(`write_files:
  27. - container: console
  28. path: /etc/rc.local
  29. permissions: "0755"
  30. owner: root
  31. content: |
  32. #!/bin/bash
  33. wait-for-docker`), "")
  34. testValidate(t, []byte(`rancher:
  35. docker:
  36. extra_args: ['--insecure-registry', 'my.registry.com']`), "")
  37. testValidate(t, []byte("bad_key: {}"), "Additional property bad_key is not allowed")
  38. testValidate(t, []byte("rancher: []"), "rancher: Invalid type. Expected: object, given: array")
  39. var fullConfig map[string]interface{}
  40. if err := util.ConvertIgnoreOmitEmpty(CloudConfig{}, &fullConfig); err != nil {
  41. t.Fatal(err)
  42. }
  43. fullConfigBytes, err := yaml.Marshal(fullConfig)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. testValidate(t, fullConfigBytes, "")
  48. }