proc_cmdline_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 proccmdline
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/http/httptest"
  20. "os"
  21. "testing"
  22. )
  23. func TestParseCmdlineCloudConfigFound(t *testing.T) {
  24. tests := []struct {
  25. input string
  26. expect string
  27. }{
  28. {
  29. "cloud-config-url=example.com",
  30. "example.com",
  31. },
  32. {
  33. "cloud_config_url=example.com",
  34. "example.com",
  35. },
  36. {
  37. "cloud-config-url cloud-config-url=example.com",
  38. "example.com",
  39. },
  40. {
  41. "cloud-config-url= cloud-config-url=example.com",
  42. "example.com",
  43. },
  44. {
  45. "cloud-config-url=one.example.com cloud-config-url=two.example.com",
  46. "two.example.com",
  47. },
  48. {
  49. "foo=bar cloud-config-url=example.com ping=pong",
  50. "example.com",
  51. },
  52. }
  53. for i, tt := range tests {
  54. output, err := findCloudConfigURL(tt.input)
  55. if output != tt.expect {
  56. t.Errorf("Test case %d failed: %s != %s", i, output, tt.expect)
  57. }
  58. if err != nil {
  59. t.Errorf("Test case %d produced error: %v", i, err)
  60. }
  61. }
  62. }
  63. func TestProcCmdlineAndFetchConfig(t *testing.T) {
  64. var (
  65. ProcCmdlineTmpl = "foo=bar cloud-config-url=%s/config\n"
  66. CloudConfigContent = "#cloud-config\n"
  67. )
  68. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. if r.Method == "GET" && r.RequestURI == "/config" {
  70. fmt.Fprint(w, CloudConfigContent)
  71. }
  72. }))
  73. defer ts.Close()
  74. file, err := ioutil.TempFile(os.TempDir(), "test_proc_cmdline")
  75. defer os.Remove(file.Name())
  76. if err != nil {
  77. t.Errorf("Test produced error: %v", err)
  78. }
  79. _, err = file.Write([]byte(fmt.Sprintf(ProcCmdlineTmpl, ts.URL)))
  80. if err != nil {
  81. t.Errorf("Test produced error: %v", err)
  82. }
  83. p := NewDatasource()
  84. p.Location = file.Name()
  85. cfg, err := p.FetchUserdata()
  86. if err != nil {
  87. t.Errorf("Test produced error: %v", err)
  88. }
  89. if string(cfg) != CloudConfigContent {
  90. t.Errorf("Test failed, response body: %s != %s", cfg, CloudConfigContent)
  91. }
  92. }