github.com/jenkins-x/jx/v2@v2.1.155/pkg/jenkins/fake/fake_jenkins.go (about)

     1  package fake
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  	"strings"
     9  	"time"
    10  
    11  	gojenkins "github.com/jenkins-x/golang-jenkins"
    12  )
    13  
    14  // FakeJenkins contains the state of the fake JenkinsClient
    15  type FakeJenkins struct {
    16  	baseURL string
    17  	Jobs    []gojenkins.Job
    18  	JobMap  map[string]*gojenkins.Job
    19  }
    20  
    21  // NewFakeJenkins creates a fake JenkinsClient that can be used in tests
    22  func NewFakeJenkins() *FakeJenkins {
    23  	return &FakeJenkins{
    24  		JobMap: map[string]*gojenkins.Job{},
    25  	}
    26  }
    27  
    28  // GetJobs returns the jobs
    29  func (j *FakeJenkins) GetJobs() ([]gojenkins.Job, error) {
    30  	return j.Jobs, nil
    31  }
    32  
    33  // GetJob gets a job by name
    34  func (j *FakeJenkins) GetJob(name string) (gojenkins.Job, error) {
    35  	for _, job := range j.Jobs {
    36  		if job.Name == name {
    37  			return job, nil
    38  		}
    39  	}
    40  	return gojenkins.Job{}, j.notFoundf("job: %s", name)
    41  }
    42  
    43  // CreateJobWithXML create a job from XML
    44  func (j *FakeJenkins) CreateJobWithXML(jobXml string, folder string) error {
    45  	folderJob := j.getOrCreateFolderJob(folder)
    46  	if folderJob != nil {
    47  		return nil
    48  	}
    49  	return j.notFoundf("job: %s", folder)
    50  }
    51  
    52  // CreateFolderJobWithXML creates a folder based job from XML
    53  func (j *FakeJenkins) CreateFolderJobWithXML(jobXml string, folder string, jobName string) error {
    54  	folderJob := j.getOrCreateFolderJob(folder)
    55  
    56  	for _, job := range folderJob.Jobs {
    57  		if job.Name == jobName {
    58  			return nil
    59  		}
    60  	}
    61  	folderJob.Jobs = append(folderJob.Jobs, gojenkins.Job{
    62  		Name: jobName,
    63  		Url:  folderJob.Url + "/job/" + folder,
    64  	})
    65  	return nil
    66  }
    67  
    68  func (j *FakeJenkins) getOrCreateFolderJob(folder string) *gojenkins.Job {
    69  	var folderJob *gojenkins.Job
    70  	for i, job := range j.Jobs {
    71  		if job.Name == folder {
    72  			folderJob = &j.Jobs[i]
    73  			break
    74  		}
    75  	}
    76  	if folderJob == nil {
    77  		j.Jobs = append(j.Jobs, gojenkins.Job{
    78  			Name: folder,
    79  			Url:  "/job/" + folder,
    80  		})
    81  		folderJob = &j.Jobs[len(j.Jobs)-1]
    82  	}
    83  	return folderJob
    84  }
    85  
    86  // GetJobURLPath gets the job URL patjh
    87  func (j *FakeJenkins) GetJobURLPath(name string) string {
    88  	return gojenkins.FullPath(name)
    89  }
    90  
    91  // IsErrNotFound returns true if the error is not found
    92  func (j *FakeJenkins) IsErrNotFound(err error) bool {
    93  	te, ok := err.(gojenkins.APIError)
    94  	return ok && te.StatusCode == 404
    95  }
    96  
    97  // BaseURL returns the server base URL
    98  func (j *FakeJenkins) BaseURL() string {
    99  	return j.baseURL
   100  }
   101  
   102  // SetHTTPClient sets the http client
   103  func (j *FakeJenkins) SetHTTPClient(*http.Client) {
   104  }
   105  
   106  // Post posts an object
   107  func (j *FakeJenkins) Post(string, url.Values, interface{}) (err error) {
   108  	return nil
   109  }
   110  
   111  // GetJobConfig gets the job config for the given name
   112  func (j *FakeJenkins) GetJobConfig(name string) (gojenkins.JobItem, error) {
   113  	return gojenkins.JobItem{}, j.notImplemented()
   114  }
   115  
   116  // GetBuild gets the build for a specific job and build number
   117  func (j *FakeJenkins) GetBuild(gojenkins.Job, int) (gojenkins.Build, error) {
   118  	return gojenkins.Build{}, j.notImplemented()
   119  }
   120  
   121  // GetLastBuild returns the last build of the job
   122  func (j *FakeJenkins) GetLastBuild(gojenkins.Job) (gojenkins.Build, error) {
   123  	return gojenkins.Build{}, j.notImplemented()
   124  }
   125  
   126  // StopBuild stops the build
   127  func (j *FakeJenkins) StopBuild(gojenkins.Job, int) error {
   128  	return nil
   129  }
   130  
   131  // GetMultiBranchJob gets a multi branch job of the given name
   132  func (j *FakeJenkins) GetMultiBranchJob(string, string, string) (gojenkins.Job, error) {
   133  	return gojenkins.Job{}, j.notImplemented()
   134  }
   135  
   136  // GetJobByPath fake
   137  func (j *FakeJenkins) GetJobByPath(names ...string) (gojenkins.Job, error) {
   138  	jobs := j.Jobs
   139  	lastIdx := len(names) - 1
   140  	for idx, name := range names {
   141  		found := false
   142  		for _, job := range jobs {
   143  			if job.Name == name {
   144  				if idx >= lastIdx {
   145  					return job, nil
   146  				}
   147  				jobs = job.Jobs
   148  				found = true
   149  				break
   150  			}
   151  		}
   152  		if !found {
   153  			break
   154  		}
   155  	}
   156  	return gojenkins.Job{}, j.notFoundf("job for path %s", strings.Join(names, "/"))
   157  }
   158  
   159  // GetOrganizationScanResult returns the organisation scan result
   160  func (j *FakeJenkins) GetOrganizationScanResult(int, gojenkins.Job) (string, error) {
   161  	return "", j.notImplemented()
   162  }
   163  
   164  // CreateJob creates a job
   165  func (j *FakeJenkins) CreateJob(gojenkins.JobItem, string) error {
   166  	return nil
   167  }
   168  
   169  // Reload reloads the fake server
   170  func (j *FakeJenkins) Reload() error {
   171  	return nil
   172  }
   173  
   174  // Restart restarts the fake server
   175  func (j *FakeJenkins) Restart() error {
   176  	return nil
   177  }
   178  
   179  // SafeRestart safely restarts the fake server
   180  func (j *FakeJenkins) SafeRestart() error {
   181  	return nil
   182  }
   183  
   184  // QuietDown quiets down
   185  func (j *FakeJenkins) QuietDown() error {
   186  	return nil
   187  }
   188  
   189  // GetCredential get the credential of the given name
   190  func (j *FakeJenkins) GetCredential(string) (*gojenkins.Credentials, error) {
   191  	return nil, j.notImplemented()
   192  }
   193  
   194  // CreateCredential creates a credential
   195  func (j *FakeJenkins) CreateCredential(string, string, string) error {
   196  	return nil
   197  }
   198  
   199  // DeleteJob deletes a job
   200  func (j *FakeJenkins) DeleteJob(gojenkins.Job) error {
   201  	return nil
   202  }
   203  
   204  // UpdateJob updates a job
   205  func (j *FakeJenkins) UpdateJob(gojenkins.JobItem, string) error {
   206  	return nil
   207  }
   208  
   209  // RemoveJob removes a job
   210  func (j *FakeJenkins) RemoveJob(string) error {
   211  	return nil
   212  }
   213  
   214  // AddJobToView adds a job to the view
   215  func (j *FakeJenkins) AddJobToView(string, gojenkins.Job) error {
   216  	return nil
   217  }
   218  
   219  // CreateView creates a view
   220  func (j *FakeJenkins) CreateView(gojenkins.ListView) error {
   221  	return nil
   222  }
   223  
   224  // Build triggers a build
   225  func (j *FakeJenkins) Build(gojenkins.Job, url.Values) error {
   226  	return nil
   227  }
   228  
   229  // GetBuildConsoleOutput get the console output
   230  func (j *FakeJenkins) GetBuildConsoleOutput(gojenkins.Build) ([]byte, error) {
   231  	return nil, j.notImplemented()
   232  }
   233  
   234  // GetQueue gets the build queue
   235  func (j *FakeJenkins) GetQueue() (gojenkins.Queue, error) {
   236  	return gojenkins.Queue{}, j.notImplemented()
   237  }
   238  
   239  // GetArtifact gets an artifact
   240  func (j *FakeJenkins) GetArtifact(gojenkins.Build, gojenkins.Artifact) ([]byte, error) {
   241  	return nil, j.notImplemented()
   242  }
   243  
   244  // SetBuildDescription sets the build description
   245  func (j *FakeJenkins) SetBuildDescription(gojenkins.Build, string) error {
   246  	return nil
   247  }
   248  
   249  // GetComputerObject gets the computer
   250  func (j *FakeJenkins) GetComputerObject() (gojenkins.ComputerObject, error) {
   251  	return gojenkins.ComputerObject{}, j.notImplemented()
   252  }
   253  
   254  // GetComputers gets the computers
   255  func (j *FakeJenkins) GetComputers() ([]gojenkins.Computer, error) {
   256  	return nil, j.notImplemented()
   257  }
   258  
   259  // GetComputer gets the computer
   260  func (j *FakeJenkins) GetComputer(string) (gojenkins.Computer, error) {
   261  	return gojenkins.Computer{}, j.notImplemented()
   262  }
   263  
   264  // GetBuildURL gets the build URL
   265  func (j *FakeJenkins) GetBuildURL(gojenkins.Job, int) string {
   266  	return ""
   267  }
   268  
   269  // GetLogFromURL gets the log from a URL
   270  func (j *FakeJenkins) GetLogFromURL(string, int64, *gojenkins.LogData) error {
   271  	return nil
   272  }
   273  
   274  // TailLog tails the log
   275  func (j *FakeJenkins) TailLog(string, io.Writer, time.Duration, time.Duration) error {
   276  	return nil
   277  }
   278  
   279  // TailLogFunc tails the log function
   280  func (j *FakeJenkins) TailLogFunc(string, io.Writer) gojenkins.ConditionFunc {
   281  	return nil
   282  }
   283  
   284  // NewLogPoller creates a new log poller
   285  func (j *FakeJenkins) NewLogPoller(string, io.Writer) *gojenkins.LogPoller {
   286  	return nil
   287  }
   288  
   289  func (j *FakeJenkins) notImplemented() error {
   290  	return fmt.Errorf("not implemented")
   291  }
   292  
   293  func (j *FakeJenkins) notFound(message string) error {
   294  	return fmt.Errorf("not found: %s", message)
   295  }
   296  
   297  func (j *FakeJenkins) notFoundf(message string, arguments ...interface{}) error {
   298  	return j.notFound(fmt.Sprintf(message, arguments...))
   299  }