2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:36:22 +04:00
|
|
|
|
2014-04-22 00:31:23 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-09-22 03:46:58 +04:00
|
|
|
|
2015-11-09 11:24:52 +03:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
2014-04-22 00:31:23 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseHeaderCRLF(t *testing.T) {
|
|
|
|
configs := []string{
|
|
|
|
"#cloud-config\nfoo: bar",
|
|
|
|
"#cloud-config\r\nfoo: bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, config := range configs {
|
|
|
|
_, err := ParseUserData(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed parsing config %d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scripts := []string{
|
|
|
|
"#!bin/bash\necho foo",
|
|
|
|
"#!bin/bash\r\necho foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, script := range scripts {
|
|
|
|
_, err := ParseUserData(script)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed parsing script %d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-05 21:49:02 +04:00
|
|
|
|
|
|
|
func TestParseConfigCRLF(t *testing.T) {
|
2015-08-28 13:38:47 +03:00
|
|
|
contents := "#cloud-config \r\nhostname: foo\r\nssh_authorized_keys:\r\n - foobar\r\n"
|
2014-05-05 21:49:02 +04:00
|
|
|
ud, err := ParseUserData(contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed parsing config: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-22 03:46:58 +04:00
|
|
|
cfg := ud.(*config.CloudConfig)
|
2014-05-05 21:49:02 +04:00
|
|
|
|
|
|
|
if cfg.Hostname != "foo" {
|
|
|
|
t.Error("Failed parsing hostname from config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.SSHAuthorizedKeys) != 1 {
|
|
|
|
t.Error("Parsed incorrect number of SSH keys")
|
|
|
|
}
|
|
|
|
}
|
2014-06-28 10:58:27 +04:00
|
|
|
|
|
|
|
func TestParseConfigEmpty(t *testing.T) {
|
|
|
|
i, e := ParseUserData(``)
|
|
|
|
if i != nil {
|
|
|
|
t.Error("ParseUserData of empty string returned non-nil unexpectedly")
|
|
|
|
} else if e != nil {
|
|
|
|
t.Error("ParseUserData of empty string returned error unexpectedly")
|
|
|
|
}
|
|
|
|
}
|