Exemple #1
0
func TestCaptureApi(t *testing.T) {
	apiServer, err := createAPIServer(t, "basic")
	if err != nil {
		t.Fatal(err)
	}

	defer apiServer.Stop()
	apiClient, err := apiServer.GetClient()
	if err != nil {
		t.Fatal(err)
	}

	capture := api.NewCapture("*/br-sflow[Type=ovsbridge]", "port 80")
	if err := apiClient.create("capture", capture); err != nil {
		t.Fatalf("Failed to create alert: %s", err.Error())
	}

	capture2 := &api.Capture{}
	if err := apiClient.Get("capture", capture.ProbePath, &capture2); err != nil {
		t.Error(err)
	}

	if *capture != *capture2 {
		t.Errorf("Capture corrupted: %+v != %+v", capture, capture2)
	}

	var captures map[string]api.Capture
	if err := apiClient.List("capture", &captures); err != nil {
		t.Error(err)
	} else {
		if len(captures) != 1 {
			t.Errorf("Wrong number of captures: got %d, expected 1", len(captures))
		}
	}

	if captures[capture.ProbePath] != *capture {
		t.Errorf("Capture corrupted: %+v != %+v", captures[capture.ProbePath], capture)
	}

	if err := apiClient.Delete("capture", capture.ProbePath); err != nil {
		t.Errorf("Failed to delete capture: %s", err.Error())
	}

	var captures2 map[string]api.Capture
	if err := apiClient.List("capture", &captures2); err != nil {
		t.Errorf("Failed to list captures: %s", err.Error())
	} else {
		if len(captures2) != 0 {
			t.Errorf("Wrong number of captures: got %d, expected 0 (%+v)", len(captures2), captures2)
		}
	}

	if err := apiClient.Get("capture", capture.ProbePath, &capture2); err == nil {
		t.Errorf("Found delete capture: %s", capture.ProbePath)
	}
}
Exemple #2
0
var CaptureCreate = &cobra.Command{
	Use:   "create",
	Short: "Create capture",
	Long:  "Create capture",
	Run: func(cmd *cobra.Command, args []string) {
		if len(probePath) == 0 {
			fmt.Println("You need to specify a probe path")
			cmd.Usage()
			os.Exit(1)
		}

		client := api.NewCrudClientFromConfig(&authenticationOpts)
		if client == nil {
			os.Exit(1)
		}
		capture := api.NewCapture(probePath, bpfFilter)
		if err := client.Create("capture", &capture); err != nil {
			logging.GetLogger().Errorf(err.Error())
			os.Exit(1)
		}
		printJSON(&capture)
	},
}

var CaptureList = &cobra.Command{
	Use:   "list",
	Short: "List captures",
	Long:  "List captures",
	Run: func(cmd *cobra.Command, args []string) {
		var captures map[string]api.Capture
		client := api.NewCrudClientFromConfig(&authenticationOpts)