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 #2
0
func main() {
	// TODO(tmrts): Add command-line flags

	// TODO(tmrts): Build a logger hierarchy
	hasRoot, err := HasRootPrivileges()
	if err != nil {
		flog.Fatal("Failed checking user privileges",
			flog.Fields{
				Event: "main.HasRootPrivileges",
				Error: err,
			},
		)
	}

	if !hasRoot {
		flog.Fatal("current user doesn't have root privileges")
	}

	centOS := distro.CentOS(sys.DefaultExecutor)

	if err := InitializeContextualization(); err != nil {
		flog.Fatal("Failed to start contextualization",
			flog.Fields{
				Event: "main.InitializeContextualization",
				Error: err,
			},
		)
	}

	providers := datasrc.SupportedProviders()

	p, err := datasrc.FindProvider(providers, 5*time.Second)
	if err != nil {
		flog.Fatal("Failed to start contextualization",
			flog.Fields{
				Event: "datasrc.FindProvider",
				Error: err,
			},
		)
	}

	m, err := p.FetchMetadata()
	if err != nil {
		flog.Fatal("Failed to fetch meta-data from provider",
			flog.Fields{
				Event: "datasrc.Provider.FetchMetadata",
				Error: err,
			},
			flog.Details{
				"provider": p,
			},
		)
	}

	u, err := p.FetchUserdata()
	if err != nil {
		flog.Fatal("Failed to fetch user-data from provider",
			flog.Fields{
				Event: "datasrc.Provider.FetchUserdata",
				Error: err,
			},
			flog.Details{
				"provider": p,
			},
		)
	}

	if err := centOS.ConsumeMetadata(m); err != nil {
		flog.Fatal("Failed to consume meta-data",
			flog.Fields{
				Event: "distro.Implementation.ConsumeMetadata",
				Error: err,
			},
			flog.Details{
				"distribution": "CentOS",
			},
		)
	}

	if err := centOS.ConsumeUserdata(u); err != nil {
		flog.Fatal("Failed to consume user-data",
			flog.Fields{
				Event: "distro.Implementation.ConsumeMetadata",
				Error: err,
			},
			flog.Details{
				"distribution": "CentOS",
			},
		)
	}

	if err := FinalizeContextualization(); err != nil {
		flog.Fatal("Failed to finalize contextualization",
			flog.Fields{
				Event: "main.FinalizeContextualization",
				Error: err,
			},
		)
	}
}