|
| 1 | +package keploy |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "go.uber.org/zap" |
| 13 | + |
| 14 | + "fmt" |
| 15 | + "os" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + logger *zap.Logger |
| 20 | +) |
| 21 | + |
| 22 | +type Config struct { |
| 23 | + Mode Mode // Keploy mode on which unit test will run. Possible values: MODE_TEST or MODE_RECORD. Default: MODE_TEST |
| 24 | + Name string // Name to record the mock or test the mocks |
| 25 | + Path string // Path in which Keploy "/mocks" will be generated. Default: current working directroy. |
| 26 | + EnableKeployLogs bool |
| 27 | + Delay int |
| 28 | +} |
| 29 | + |
| 30 | +func New(conf Config) error { |
| 31 | + |
| 32 | + var ( |
| 33 | + mode = MODE_OFF |
| 34 | + err error |
| 35 | + path string = conf.Path |
| 36 | + keployCmd string |
| 37 | + delay int = 5 |
| 38 | + ) |
| 39 | + |
| 40 | + logger, _ = zap.NewDevelopment() |
| 41 | + defer func() { |
| 42 | + _ = logger.Sync() |
| 43 | + }() |
| 44 | + |
| 45 | + // killing keploy instance if it is running already |
| 46 | + KillProcessOnPort() |
| 47 | + |
| 48 | + if Mode(conf.Mode).Valid() { |
| 49 | + mode = Mode(conf.Mode) |
| 50 | + } else { |
| 51 | + return errors.New("provided keploy mode is invalid, either use MODE_RECORD/MODE_TEST/MODE_OFF") |
| 52 | + } |
| 53 | + |
| 54 | + if conf.Delay > 5 { |
| 55 | + delay = conf.Delay |
| 56 | + } |
| 57 | + |
| 58 | + if mode == MODE_OFF { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + // use current directory, if path is not provided or relative in config |
| 63 | + if path == "" { |
| 64 | + path, err = os.Getwd() |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("no specific path provided and failed to get current working directory %w", err) |
| 67 | + } |
| 68 | + logger.Info("no specific path provided; defaulting to the current working directory", zap.String("currentDirectoryPath", path)) |
| 69 | + } else if path[0] != '/' { |
| 70 | + path, err = filepath.Abs(path) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to get the absolute path from provided path %w", err) |
| 73 | + } |
| 74 | + } else { |
| 75 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 76 | + return fmt.Errorf("provided path does not exist %w", err) |
| 77 | + } |
| 78 | + logger.Info("using provided path to store mocks", zap.String("providedPath", path)) |
| 79 | + } |
| 80 | + |
| 81 | + if conf.Name == "" { |
| 82 | + return errors.New("provided mock name is empty") |
| 83 | + } |
| 84 | + |
| 85 | + if mode == MODE_RECORD { |
| 86 | + if _, err := os.Stat(path + "/stubs/" + conf.Name + "-mocks.yaml"); !os.IsNotExist(err) { |
| 87 | + cmd := exec.Command("sudo", "rm", "-rf", path+"/stubs/"+conf.Name+"-mocks.yaml") |
| 88 | + _, err := cmd.CombinedOutput() |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to replace existing mock file %w", err) |
| 91 | + } |
| 92 | + } |
| 93 | + if _, err := os.Stat(path + "/stubs/" + conf.Name + "-config.yaml"); !os.IsNotExist(err) { |
| 94 | + cmd := exec.Command("sudo", "rm", "-rf", path+"/stubs/"+conf.Name+"-config.yaml") |
| 95 | + _, err := cmd.CombinedOutput() |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to replace existing mock file %w", err) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + appPid := os.Getpid() |
| 103 | + |
| 104 | + recordCmd := "sudo -E /usr/local/bin/keploy mockRecord --pid " + strconv.Itoa(appPid) + " --path " + path + " --mockName " + conf.Name + " --debug" |
| 105 | + testCmd := "sudo -E /usr/local/bin/keploy mockTest --pid " + strconv.Itoa(appPid) + " --path " + path + " --mockName " + conf.Name + " --debug" |
| 106 | + |
| 107 | + if mode == MODE_TEST { |
| 108 | + keployCmd = testCmd |
| 109 | + } else { |
| 110 | + keployCmd = recordCmd |
| 111 | + } |
| 112 | + |
| 113 | + parts := strings.Fields(keployCmd) |
| 114 | + cmd := exec.Command(parts[0], parts[1:]...) |
| 115 | + if conf.EnableKeployLogs { |
| 116 | + cmd.Stdout = os.Stdout |
| 117 | + cmd.Stderr = os.Stderr |
| 118 | + } |
| 119 | + |
| 120 | + if _, err := exec.LookPath("keploy"); err != nil { |
| 121 | + return fmt.Errorf("keploy binary not found, please ensure it is installed. Host OS: %s, Architecture: %s. For installing please follow instructions https://github.com/keploy/keploy#quick-installation", runtime.GOOS, runtime.GOARCH) |
| 122 | + } |
| 123 | + |
| 124 | + errChan := make(chan error) |
| 125 | + |
| 126 | + go func() { |
| 127 | + err := cmd.Run() |
| 128 | + if err != nil { |
| 129 | + errChan <- err |
| 130 | + } |
| 131 | + close(errChan) |
| 132 | + }() |
| 133 | + |
| 134 | + select { |
| 135 | + case err := <-errChan: |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + return nil |
| 140 | + case <-time.After(time.Duration(delay) * time.Second): |
| 141 | + return nil |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func KillProcessOnPort() { |
| 146 | + port := 16789 |
| 147 | + cmd := exec.Command("sudo", "lsof", "-t", "-i:"+strconv.Itoa(port)) |
| 148 | + output, err := cmd.Output() |
| 149 | + if _, ok := err.(*exec.ExitError); ok && len(output) == 0 { |
| 150 | + return |
| 151 | + } else if err != nil { |
| 152 | + logger.Error("Failed to execute lsof: %v\n", zap.Error(err)) |
| 153 | + return |
| 154 | + } |
| 155 | + appPid := os.Getpid() |
| 156 | + pids := strings.Split(strings.Trim(string(output), "\n"), "\n") |
| 157 | + for _, pid := range pids { |
| 158 | + if pid != strconv.Itoa(appPid) { |
| 159 | + forceKillProcessByPID(pid) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func forceKillProcessByPID(pid string) { |
| 165 | + cmd := exec.Command("sudo", "kill", "-9", pid) |
| 166 | + if err := cmd.Run(); err != nil { |
| 167 | + logger.Error(fmt.Sprintf("Failed to kill process with PID %s:", pid), zap.Error(err)) |
| 168 | + } |
| 169 | +} |
0 commit comments