user_data_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package initialize
  15. import (
  16. "testing"
  17. "github.com/rancher/os/config/cloudinit/config"
  18. )
  19. func TestParseHeaderCRLF(t *testing.T) {
  20. configs := []string{
  21. "#cloud-config\nfoo: bar",
  22. "#cloud-config\r\nfoo: bar",
  23. }
  24. for i, config := range configs {
  25. _, err := ParseUserData(config)
  26. if err != nil {
  27. t.Errorf("Failed parsing config %d: %v", i, err)
  28. }
  29. }
  30. scripts := []string{
  31. "#!bin/bash\necho foo",
  32. "#!bin/bash\r\necho foo",
  33. }
  34. for i, script := range scripts {
  35. _, err := ParseUserData(script)
  36. if err != nil {
  37. t.Errorf("Failed parsing script %d: %v", i, err)
  38. }
  39. }
  40. }
  41. func TestParseConfigCRLF(t *testing.T) {
  42. contents := "#cloud-config \r\nhostname: foo\r\nssh_authorized_keys:\r\n - foobar\r\n"
  43. ud, err := ParseUserData(contents)
  44. if err != nil {
  45. t.Fatalf("Failed parsing config: %v", err)
  46. }
  47. cfg := ud.(*config.CloudConfig)
  48. if cfg.Hostname != "foo" {
  49. t.Error("Failed parsing hostname from config")
  50. }
  51. if len(cfg.SSHAuthorizedKeys) != 1 {
  52. t.Error("Parsed incorrect number of SSH keys")
  53. }
  54. }
  55. func TestParseConfigEmpty(t *testing.T) {
  56. i, e := ParseUserData(``)
  57. if i != nil {
  58. t.Error("ParseUserData of empty string returned non-nil unexpectedly")
  59. } else if e != nil {
  60. t.Error("ParseUserData of empty string returned error unexpectedly")
  61. }
  62. }