validate_test.go 1.3 KB

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