<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Viru on Medium]]></title>
        <description><![CDATA[Stories by Viru on Medium]]></description>
        <link>https://medium.com/@virup?source=rss-5530d76b3130------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*XN5jPtXl58Cx7IFJ.jpg</url>
            <title>Stories by Viru on Medium</title>
            <link>https://medium.com/@virup?source=rss-5530d76b3130------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@virup/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Helm Charts Cheatsheet]]></title>
            <link>https://medium.com/@virup/helmcharts-9bd277dcf4d5?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bd277dcf4d5</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[helm]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Tue, 16 Apr 2019 06:31:58 GMT</pubDate>
            <atom:updated>2019-04-16T06:46:10.639Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*yDShGWZKpVqcS_lTlPQUvQ.png" /></figure><p>Helm is a package manager like <em>yum</em> or <em>apt-get for Kubernetes based apps.</em><br>You can download apps using helm and run them.</p><p><strong>Charts</strong> — (analogous to yum package name) consist of an app</p><p><strong>Repository</strong> — (analogous to yum repositories) where you can download an app</p><p><strong>Release</strong> — a running app</p><p><strong>Initialize Helm:</strong></p><pre>helm init</pre><p><strong>List available Helm charts:</strong></p><pre>helm search</pre><p><strong>Filter available Helm charts:</strong></p><p><em>(only charts with “mysql” will be listed)</em></p><pre>helm search mysql</pre><p><strong>Install a helm chart:</strong></p><p><em>This installs a Helm chart in the namespace “demo” using the values file called “values.yaml”</em></p><pre>helm install stable/mysql --namespace demo -f values.yaml</pre><p><strong>Show the Kubernetes specs in a Helm chart release:</strong></p><pre>helm get manifest &lt;release name&gt;</pre><p><strong>Show the Helm charts with the substituted values but not actually install:</strong></p><pre>helm install --debug --dry-run &lt;chart&gt;</pre><p><strong>Show the Helm version</strong></p><pre>Helm version</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bd277dcf4d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Method to Mocking Functions in Go for Tests]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@virup/simple-method-to-mocking-functions-in-go-for-tests-d91ca08f5873?source=rss-5530d76b3130------2"><img src="https://cdn-images-1.medium.com/max/2600/0*AOSa09IcO3JWFXvI." width="5760"></a></p><p class="medium-feed-snippet">An easy way to mock a Go function is to override the function with a different function. This is useful when trying to mock simple&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@virup/simple-method-to-mocking-functions-in-go-for-tests-d91ca08f5873?source=rss-5530d76b3130------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@virup/simple-method-to-mocking-functions-in-go-for-tests-d91ca08f5873?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/d91ca08f5873</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[fake]]></category>
            <category><![CDATA[mock]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Mon, 30 Apr 2018 06:44:58 GMT</pubDate>
            <atom:updated>2018-04-30T07:07:32.530Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Shared Library or Service?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@virup/libraries-versus-service-723ca2cb2e98?source=rss-5530d76b3130------2"><img src="https://cdn-images-1.medium.com/max/2600/0*jQYhGa90sBfpaZOJ." width="5895"></a></p><p class="medium-feed-snippet">Recently at work, we needed a way for all our components to access kubernetes. We had to decide between having a standalone service which&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@virup/libraries-versus-service-723ca2cb2e98?source=rss-5530d76b3130------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@virup/libraries-versus-service-723ca2cb2e98?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/723ca2cb2e98</guid>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[services]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[libraries]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 07:57:20 GMT</pubDate>
            <atom:updated>2018-03-21T08:02:25.027Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to write concise tests — Table Driven Tests]]></title>
            <link>https://medium.com/@virup/how-to-write-concise-tests-table-driven-tests-ed672c502ae4?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed672c502ae4</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[table-driven]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[unittest]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Sun, 11 Mar 2018 07:13:09 GMT</pubDate>
            <atom:updated>2018-03-11T07:13:09.209Z</atom:updated>
            <content:encoded><![CDATA[<p>Table driven tests is a way to write concise tests which can later be easily extended with new test cases. Table driven tests are common in Go (though not unique to it) and a number of standard libraries¹ have these kind of tests. Table driven tests make use of anonymous structs.</p><p>In this article I will show you how to write table driven tests. Continuing with the <a href="https://github.com/virup/errline">errline repo</a>, we will introduce tests for theWrap() function. This function annotates an errorwith a file and line number at the point where Wrap()was called. In particular, we want to write a test for the logic which computes the short name of the file (highlighted in bold). Originally, the Wrap()function looks like this.</p><pre>func Wrap(err error) error {<br>    if err == nil {<br>        return nil<br>    }<br>    // If error already has file line do not add it again.<br>    if _, ok := err.(*withFileLine); ok {<br>        return err<br>    }</pre><pre>    _, file, line, ok := runtime.Caller(calldepth)<br>    if !ok {<br>        file = &quot;???&quot;<br>        line = 0<br>    }</pre><pre><strong>    short := file<br>    for i := len(file) - 1; i &gt; 0; i-- {<br>        if file[i] == &#39;/&#39; {<br>            short = file[i+1:]<br>            break<br>        }<br>    }<br>    file = short</strong><br>    return &amp;withFileLine{err, file, line}<br>}</pre><p>In order to test the short name computation logic, it is convenient to extract that logic into a separate function called getShortFilename(). Now, the code looks like this.</p><pre>func Wrap(err error) error {<br>    if err == nil {<br>        return nil<br>    }<br>    // If error already has file line do not add it again.<br>    if _, ok := err.(*withFileLine); ok {<br>        return err<br>    }</pre><pre>    _, file, line, ok := runtime.Caller(calldepth)<br>    if !ok {<br>        file = &quot;???&quot;<br>        line = 0<br>    }</pre><pre>    file = getShortFilename(file)<br>    return &amp;withFileLine{err, file, line}<br>}</pre><pre>func <strong>getShortFilename(file string)</strong> string {<br>    short := file<br>    for i := len(file) - 1; i &gt; 0; i-- {<br>        if file[i] == &#39;/&#39; {<br>            short = file[i+1:]<br>            break<br>        }<br>    }<br>    file = short<br>    return file<br>}</pre><p>It is pretty common to find ourselves refactoring our code to make it testable.</p><p>We will now test the function getShortFilename() by passing various file names and verifying that the output matches the expectation.</p><p>We start off with an empty test function</p><pre>func TestShortFilename(t *testing.T) {<br>}</pre><p>Next, we introduce an anonymous struct with two fields in and expected. in represents the input to the getShortFilename() function and expected is the expected return of that function. tests is an array of such structs.</p><pre>func TestShortFilename(t *testing.T) {<br>    tests := []struct {<br>        in       string   // input<br>        expected string   // expected result<br>    }{<br>        {&quot;???&quot;, &quot;???&quot;},<br>        {&quot;filename.go&quot;, &quot;filename.go&quot;},<br>        {&quot;hello/filename.go&quot;, &quot;filename.go&quot;},<br>        {&quot;main/hello/filename.go&quot;, &quot;filename.go&quot;},<br>    }<br>}</pre><p>With this, we can run over the test cases in a loop in our test function.</p><pre>func TestShortFilename(t *testing.T) {<br>    tests := []struct {<br>        in       string<br>        expected string<br>    }{<br>        {&quot;???&quot;, &quot;???&quot;},<br>        {&quot;filename.go&quot;, &quot;filename.go&quot;},<br>        {&quot;hello/filename.go&quot;, &quot;filename.go&quot;},<br>        {&quot;main/hello/filename.go&quot;, &quot;filename.go&quot;},<br>    }</pre><pre>    for _, tt := range tests {<br>        actual := getShortFilename(tt.in)<br>        if strings.Compare(actual, tt.expected) != 0 {<br>            t.Fail()<br>        }<br>    }<br>}</pre><p>Note, that adding another test case is pretty simply - just add another item in the tests array.</p><p>This scheme can be extended to functions which accept and return multiple values.</p><p>That is it!</p><p>This code can be accessed at <a href="https://github.com/virup/errline/tree/master">my github</a>.</p><h4><strong>References:</strong></h4><ol><li>Some examples of table driven tests in standard go library</li></ol><ul><li><a href="https://github.com/golang/go/blob/master/src/strconv/ftoa_test.go">https://github.com/golang/go/blob/master/src/strconv/ftoa_test.go</a></li><li><a href="https://github.com/golang/go/blob/master/src/path/match_test.go">https://github.com/golang/go/blob/master/src/path/match_test.go</a></li><li><a href="https://github.com/golang/go/blob/master/src/archive/tar/strconv_test.go">https://github.com/golang/go/blob/master/src/archive/tar/strconv_test.go</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed672c502ae4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing in Go — Making Use of Duck Typing]]></title>
            <link>https://medium.com/@virup/testing-in-go-making-use-of-duck-typing-6927feb125c6?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/6927feb125c6</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[duck-typing]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Sat, 03 Mar 2018 00:23:00 GMT</pubDate>
            <atom:updated>2018-03-03T00:23:00.874Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I found myself writing a test for a function which takes in an struct object from an external package. The function I was trying to test is this.</p><pre><strong>func </strong>(w *withFileLine) Format(s fmt.State, verb rune) {<br>   <strong>switch </strong>verb {<br>   <strong>case &#39;v&#39;</strong>:<br>      <strong>if </strong>s.Flag(<strong>&#39;+&#39;</strong>) {<br>         fmt.Fprintf(s, <strong>&quot;%s:%d: %+v&quot;</strong>, w.file, w.line, w.Cause())<br>         <strong>return<br>      </strong>}<br>      <strong>fallthrough<br>   case &#39;s&#39;</strong>:<br>      io.WriteString(s, w.Error())<br>   <strong>case &#39;q&#39;</strong>:<br>      fmt.Fprintf(s, <strong>&quot;%q&quot;</strong>, w.Error())<br>   }<br>}</pre><p>In my test, I want to invoke the Format() function and pass in various cases of the verb rune and verify the output. I do not have access to create an object of fmt.State to pass to this function. Note, that the Format() function does not return a string which I can verify. It actually writes out a string to the s object.</p><p>In order to create the fmt.State object, I made use of Golang’s <a href="https://medium.com/@matryer/golang-advent-calendar-day-one-duck-typing-a513aaed544d">duck typing</a>.</p><blockquote>If [a struct] has a set of methods that match an interface, then you can use it wherever that interface is needed without explicitly defining that your types implement that interface.</blockquote><p>The fmt.State interface looks like this</p><pre><strong>type </strong>State <strong>interface </strong>{<em><br>   </em>Write(b []byte) (n int, err error)<em><br>   </em>Width() (wid int, ok bool)<em><br>   </em>Precision() (prec int, ok bool)<br><em>   </em>Flag(c int) bool<br>}</pre><p>Any struct which implements these methods will be of interface State and can be passed as the first argument to the Format(s fmt.State, verb rune function.</p><p>I created a new struct fakeFmtState{}</p><pre><strong>type </strong>fakeFmtState <strong>struct</strong>{}</pre><p>Then I introduced methods on fakeFmtState to satisfy the interface of fmt.State.</p><pre><strong>var </strong>buf []byte<br><strong>func </strong>(fakeFmtState) Write(b []byte) (n int, err error) {<br>   buf = b<br>   <strong>return </strong>len(string(b)), nil<br>}<br><br><strong>func </strong>(fakeFmtState) Width() (wid int, ok bool) {<br>   <strong>return </strong>-1, <strong><em>false<br></em></strong>}<br><br><strong>func </strong>(fakeFmtState) Precision() (prec int, ok bool) {<br>   <strong>return </strong>-1, <strong><em>false<br></em></strong>}<br><br><strong>func </strong>(fakeFmtState) Flag(c int) bool {<br>   <strong>return </strong>c == <strong>&#39;+&#39;<br></strong>}</pre><p>The Write() function simply updates a global byte slice. The functions Width() and Precision() are empty. The Flag() function returns true only if the argument passed is a +. This is enough for our testing.</p><p>Next, I create a test function and pass an object of fakeFmtState to it.</p><pre><strong>func </strong>TestFormat(t *testing.T) {<br>   <em>// Setup the input to test function<br>   </em>e := withFileLine{errors.New(<strong>&quot;random err&quot;</strong>), <strong>&quot;file.go&quot;</strong>, 23}<br>   <br>   <em>// Set up the expected output<br>   </em>expected := <strong>&quot;file.go:23: random err&quot;<br>   <br>   </strong><em>// Run the function to test. Note, I pass an empty fakeFmtState<br>   </em>e.Format(fakeFmtState{}, <strong>&#39;v&#39;</strong>)<br>   <br>   <em>// Verify the output<br>   </em><strong>if </strong>string(buf) != expected {<br>      t.Fail()<br>   }<br>}</pre><p>That is it!</p><p>This code can be accessed at <a href="https://github.com/virup/errline/tree/master">my github</a>.</p><p>Go allows mocking structs even if it comes from an external package. Using this idea, I will show in a subsequent blog on how to add middlewares to perform operations like logging or collecting metrics.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6927feb125c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker Cheatsheet]]></title>
            <link>https://medium.com/@virup/docker-cheatsheet-4fbe3e45ea8d?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fbe3e45ea8d</guid>
            <category><![CDATA[cheatsheet]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Sun, 02 Apr 2017 02:06:15 GMT</pubDate>
            <atom:updated>2017-04-02T02:06:15.124Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>List docker containers :</strong></p><pre>docker ps -a</pre><p><strong>Stop docker container :</strong></p><pre>docker stop &lt;container name or container ID&gt;</pre><p><strong>Remove docker container:</strong></p><pre>docker rm containerID</pre><p><strong>List docker images :</strong></p><pre>docker images</pre><p><strong>Remove docker images:</strong></p><pre>docker rmi ImageID</pre><p><strong>Stop all containers:</strong></p><pre>docker stop $(docker ps -a -q)</pre><p><strong>Remove all containers:</strong></p><pre> docker rm $(docker ps -a -q)</pre><p><strong>Remove all images:</strong></p><pre>docker rmi $(docker images -q)</pre><p><strong>Find the IP of container:</strong></p><pre>docker inspect --format &#39;{{ .NetworkSettings.IPAddress }}&#39; ${ContainerID}</pre><p><strong>Find the IP of all the container :</strong></p><pre>docker inspect -f &#39;{{.Name}} - {{.NetworkSettings.IPAddress }}&#39; $(docker ps -aq)</pre><p><strong>Find the ports of container:</strong></p><pre>docker port &lt;containerID&gt;</pre><p><strong>Find the host port of container:</strong></p><p>For example, to find the host port for the container port <strong>8080</strong></p><pre>docker inspect — format=’{{(index (index .NetworkSettings.Ports “8080/tcp”) 1).HostPort}}’ &lt;Container ID&gt;</pre><p><strong>Clean system:</strong></p><p>This stops all docker containers, removes them and then deletes all docker images</p><pre>docker stop $(docker ps -a -q); docker rm $(docker ps -a -q); docker rmi $(docker images -q)</pre><p><strong>Remove unused Docker Images:</strong></p><pre>docker rmi $(docker images -a|awk ‘/none/ { print $3 }’)</pre><p><strong>Check if container running as privileged :</strong></p><pre>docker inspect --format=&#39;{{.HostConfig.Privileged}}&#39;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fbe3e45ea8d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes Cheatsheet]]></title>
            <link>https://medium.com/@virup/kubernetes-cheatsheet-7bffb258f1ce?source=rss-5530d76b3130------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bffb258f1ce</guid>
            <category><![CDATA[commands]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cheatsheet]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Viru]]></dc:creator>
            <pubDate>Mon, 17 Oct 2016 04:03:51 GMT</pubDate>
            <atom:updated>2016-10-17T04:03:51.819Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>List all nodes:</strong></p><pre>kubectl get nodes</pre><p><strong>List the namespaces:</strong></p><pre>kubectl get namespaces</pre><p><strong>List the pods :</strong></p><pre>kubectl get pods --namespace=&lt;namespace&gt;</pre><p><strong>Check status of pod:</strong></p><pre>kubectl get pods --all-namespaces</pre><pre>kubectl get pods --namespace=&lt;namespace&gt;</pre><p><strong>Look at the logs of the container :</strong></p><pre>kubectl logs --namespace=&lt;namespace&gt; pod_name</pre><p><strong>Follow the logs of the container:</strong></p><pre>kubectl logs -f --namespace=&lt;namespace&gt; &lt;pod_name&gt;</pre><p><strong>All the logs for a docker container:</strong></p><pre>sudo docker logs &lt;containerID&gt;</pre><p><strong>Delete everything under a namespace:</strong></p><pre>kubectl delete namespaces &lt;insert-some-namespace-name&gt;</pre><p><strong>All the logs for a minion:</strong></p><pre>journalctl -u kubelet -f</pre><p><strong>Delete services :</strong></p><pre>kubectl delete services oracle --namespace=&lt;namespace&gt;</pre><p><strong>Restart kubelet:</strong></p><pre>systemctl restart kubelet</pre><pre>systemctl restart kube-proxy</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bffb258f1ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>