func TestRefreshAvailableVersions(t *testing.T) { desc := "this is test1" updAvail := "nothing" expectedCompVsns := operations.GetComponentsByLatestReleaseOKBodyBody{ Data: []*models.ComponentVersion{ &models.ComponentVersion{ Component: &models.Component{Name: "test1", Description: &desc}, Version: &models.Version{Train: "testTrain"}, UpdateAvailable: &updAvail, }, }, } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") if err := json.NewEncoder(w).Encode(expectedCompVsns); err != nil { http.Error(w, "error encoding JSON", http.StatusInternalServerError) return } })) defer ts.Close() apiclient, err := config.GetSwaggerClient(ts.URL) assert.NoErr(t, err) vsns := availableVersionsFromAPI{ rwm: new(sync.RWMutex), baseVersionsURL: ts.URL, apiClient: apiclient, } retCompVsns, err := vsns.Refresh(models.Cluster{}) assert.NoErr(t, err) assert.Equal(t, len(retCompVsns), len(expectedCompVsns.Data), "number of component versions") }
func main() { kubeClient, err := kcl.NewInCluster() if err != nil { log.Fatalf("Error creating new Kubernetes client (%s)", err) } apiClient, err := config.GetSwaggerClient(config.Spec.VersionsAPIURL) if err != nil { log.Fatalf("Error creating new swagger api client (%s)", err) } deisK8sResources := k8s.NewResourceInterfaceNamespaced(kubeClient, config.Spec.DeisNamespace) clusterID := data.NewClusterIDFromPersistentStorage(deisK8sResources.Secrets()) installedDeisData := data.NewInstalledDeisData(deisK8sResources) availableVersion := data.NewAvailableVersionsFromAPI( apiClient, config.Spec.VersionsAPIURL, ) availableComponentVersion := data.NewLatestReleasedComponent(deisK8sResources, availableVersion) pollDur := time.Duration(config.Spec.Polling) * time.Second // we want to do the following jobs according to our remote API interval: // 1. get latest stable deis component versions // 2. send diagnostic data, if appropriate glvdPeriodic := jobs.NewGetLatestVersionDataPeriodic( installedDeisData, clusterID, availableVersion, availableComponentVersion, pollDur, ) svPeriodic := jobs.NewSendVersionsPeriodic( apiClient, clusterID, deisK8sResources, availableVersion, pollDur, ) toDo := []jobs.Periodic{glvdPeriodic, svPeriodic} log.Printf("Starting periodic jobs at interval %s", pollDur) ch := jobs.DoPeriodic(toDo) defer close(ch) // Get a new router, with handler functions r := handlers.RegisterRoutes(mux.NewRouter(), availableVersion, deisK8sResources) // Bind to a port and pass our router in hostStr := fmt.Sprintf(":%s", config.Spec.Port) log.Printf("Serving on %s", hostStr) if err := http.ListenAndServe(hostStr, r); err != nil { close(ch) log.Println("Unable to open up TLS listener") log.Fatal("ListenAndServe: ", err) } }
func getWfmMockAPIClient(respBody []byte) (*apiclient.WorkflowManager, *httptest.Server, error) { apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") if err := json.NewEncoder(w).Encode(respBody); err != nil { http.Error(w, "error encoding JSON", http.StatusInternalServerError) return } })) apiClient, err := config.GetSwaggerClient(apiServer.URL) if err != nil { return nil, nil, err } return apiClient, apiServer, nil }
func TestDoctorHandler(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") if err := json.NewEncoder(w).Encode([]byte("")); err != nil { http.Error(w, "error encoding JSON", http.StatusInternalServerError) return } })) defer ts.Close() apiClient, err := config.GetSwaggerClient(ts.URL) doctorHandler := DoctorHandler( mockInstalledComponents{}, mockRunningK8sData{}, // TODO: mock k8s node data &mockClusterID{}, mockAvailableVersion{}, apiClient, ) resp, err := getTestHandlerResponse(doctorHandler) assert.NoErr(t, err) assert200(t, resp) respData, err := ioutil.ReadAll(resp.Body) assert.NoErr(t, err) // verify that the return data is a uuid string doctorID1, err := uuid.FromString(string(respData[:])) assert.NoErr(t, err) // invoke the handler a 2nd time to ensure that unique IDs are created for // each request resp2, err := getTestHandlerResponse(doctorHandler) assert.NoErr(t, err) assert200(t, resp2) respData2, err := ioutil.ReadAll(resp2.Body) assert.NoErr(t, err) doctorID2, err := uuid.FromString(string(respData2[:])) assert.NoErr(t, err) if doctorID1 == doctorID2 { t.Error("DoctorHandler should return a unique ID for every invocation") } }
// RegisterRoutes attaches handler functions to routes func RegisterRoutes( r *mux.Router, availVers data.AvailableVersions, k8sResources *k8s.ResourceInterfaceNamespaced, ) *mux.Router { clusterID := data.NewClusterIDFromPersistentStorage(k8sResources.Secrets()) r.Handle(componentsRoute, ComponentsHandler( data.NewInstalledDeisData(k8sResources), clusterID, data.NewLatestReleasedComponent(k8sResources, availVers), )) r.Handle(idRoute, IDHandler(clusterID)) doctorAPIClient, _ := config.GetSwaggerClient(config.Spec.DoctorAPIURL) r.Handle(doctorRoute, DoctorHandler( data.NewInstalledDeisData(k8sResources), k8s.NewRunningK8sData(k8sResources), clusterID, data.NewLatestReleasedComponent(k8sResources, availVers), doctorAPIClient, )).Methods("POST") return r }