validate_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 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. }