Beispiel #1
0
func TestRetrievesDataFromEC2(t *testing.T) {
	Convey("Given an EC2 meta-data service", t, func() {
		// mock EC2 metadata server
		server := NewMockServer(func(w http.ResponseWriter, r *http.Request) {
			attributes := map[string]string{
				"hostname":                  "centos.ec2",
				"local-ipv4":                "10.240.51.29",
				"public-ipv4":               "104.155.21.99",
				"public-keys/0/openssh-key": "ssh-rsa OPENSSH_KEY",
			}

			if strings.Contains(r.URL.String(), "/2009-04-04/meta-data/") {
				for attr, value := range attributes {
					if strings.HasSuffix(r.URL.String(), attr) {
						w.Write([]byte(value))
					}
				}
			} else if strings.HasSuffix(r.URL.String(), "/2009-04-04/user-data") {
				w.Write([]byte("#cloud-config\n"))
			} else {
				http.Error(w, "requested resource is not found", http.StatusNotFound)
			}
		})

		service := ec2.MetadataService{
			URL: provider.FormatURL(server.URL + "/%v/%v/%v"),
		}

		Convey("It should retrieve meta-data from EC2 meta-data service", func() {
			digest, err := service.FetchMetadata()
			So(err, ShouldBeNil)

			So(digest.Hostname, ShouldEqual, "centos.ec2")

			ifc := digest.PrimaryNetworkInterface()
			So(ifc.PublicIPs[0].String(), ShouldEqual, "104.155.21.99")

			sshKeys := digest.SSHKeys

			So(sshKeys["root"], ShouldConsistOf, ssh.Key("ssh-rsa OPENSSH_KEY"))
		})

		Convey("It should retrieve user-data from EC2 meta-data service", func() {
			userdata, err := service.FetchUserdata()
			So(err, ShouldBeNil)

			So(userdata["user-data"], ShouldEqual, "#cloud-config\n")
		})

	})
}
func TestFetchsMetadata(t *testing.T) {
	Convey("Given a list of datasources and a timeout duration", t, func() {
		mockGCEServer := testutil.NewMockServer(func(w http.ResponseWriter, r *http.Request) {
			var json_path string
			if r.Header.Get("Metadata-Flavor") != "Google" {
				http.Error(w, "metadata header is not found", http.StatusBadRequest)
				return
			}

			testMetadataDir := "provider/gce/test_metadata"
			if strings.Contains(r.URL.String(), "project") {
				json_path = filepath.Join(testMetadataDir, "GCEv1_project.json")
			} else if strings.Contains(r.URL.String(), "instance") {
				json_path = filepath.Join(testMetadataDir, "GCEv1_instance.json")
			} else {
				http.Error(w, "requested resource is not found", http.StatusNotFound)
				return
			}

			buf, err := ioutil.ReadFile(json_path)
			if err != nil {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}

			w.Write(buf)
		})

		mockGCEProvider := &gce.MetadataService{
			URL: provider.FormatURL(mockGCEServer.URL + "/%v/%v"),
		}

		mockDataSources := map[string]datasrc.Provider{
			"gce": mockGCEProvider,
		}

		timeout := time.Millisecond * 500

		Convey("It should find the available data source provider", func() {
			provider, err := datasrc.FindProvider(mockDataSources, timeout)
			So(err, ShouldBeNil)

			So(provider, ShouldEqual, mockGCEProvider)
		})

		Convey("When datasources are unavailable it should timeout", func() {
			_, err := datasrc.FindProvider(map[string]datasrc.Provider{}, 0*time.Second)
			So(err, ShouldEqual, datasrc.ErrDatasourceRetrievalTimeout)
		})
	})
}
Beispiel #3
0
func TestGoogleComputeMetadataRetrieval(t *testing.T) {
	Convey("Given a GCE meta-data service", t, func() {
		// mock GCE metadata server
		server := NewMockServer(func(w http.ResponseWriter, r *http.Request) {
			var json_path string
			if r.Header.Get("Metadata-Flavor") != "Google" {
				http.Error(w, "metadata header is not found", http.StatusBadRequest)
				return
			}

			if strings.Contains(r.URL.String(), "project") {
				json_path = filepath.Join(testMetadataDir, "GCEv1_project.json")
			} else if strings.Contains(r.URL.String(), "instance") {
				json_path = filepath.Join(testMetadataDir, "GCEv1_instance.json")
			} else {
				http.Error(w, "requested resource is not found", http.StatusNotFound)
				return
			}

			buf, err := ioutil.ReadFile(json_path)
			if err != nil {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}

			w.Write(buf)
		})

		service := gce.MetadataService{
			URL: provider.FormatURL(server.URL + "/%v/%v"),
		}

		Convey("It should retrieve meta-data from GCE meta-data service", func() {
			digest, err := service.FetchMetadata()
			So(err, ShouldBeNil)

			So(digest.Hostname, ShouldEqual, "centos.internal")

			ifc := digest.PrimaryNetworkInterface()
			So(ifc.PrivateIP.String(), ShouldEqual, "10.240.45.128")

			So(ifc.PublicIPs[0].String(), ShouldEqual, "104.155.21.159")
			So(ifc.PublicIPs[1].String(), ShouldEqual, "104.155.21.160")

			sshKeys := digest.SSHKeys

			So(sshKeys["user1"], ShouldConsistOf,
				ssh.Key("ssh-rsa RSA_PUBLIC_KEY_FOR_USER_1 user1@machine"),
				ssh.Key("ssh-dsa DSA_PUBLIC_KEY_FOR_USER_1 user1@machine"))

			So(sshKeys["user2"], ShouldConsistOf,
				ssh.Key("ssh-rsa RSA_PUBLIC_KEY_FOR_USER_2 user2@machine"))
		})

		Convey("It should retrieve user-data from GCE meta-data service", func() {
			userdata, err := service.FetchUserdata()
			So(err, ShouldBeNil)

			So(userdata["user-data"], ShouldEqual, "#cloud-config\n")
		})

	})
}