validate_test.go 1.4 KB

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